This commit is contained in:
Yanick Champoux 2018-12-04 09:57:04 -05:00
parent 8ecca99278
commit fea5704737
3 changed files with 1200 additions and 0 deletions

1134
2018/04/input.txt Normal file

File diff suppressed because it is too large Load Diff

49
2018/04/sol1.pl Normal file
View File

@ -0,0 +1,49 @@
use 5.20.0;
use warnings;
use List::AllUtils qw/ before after_incl indexes sum pairs pairmap first /;
use List::UtilsBy qw/ partition_by max_by /;
use experimental qw/
signatures
postderef
/;
my @entries = sort <>;
say $_ for @entries;
my %sleep;
my $guard;
my $start;
for ( @entries ) {
if( /#(\d+)/ ) { $guard = $1; }
elsif( /0?(\d+)\] falls asleep/ ) { $start = $1; }
elsif( /0?(\d+)\] wakes/ ) {
push $sleep{$guard}->@*, [ $start, $1 ];
}
}
use DDP;
my %nappers = pairmap { $a => sum map { $_->[1] - $_->[0] } @$b } %sleep;
p %nappers;
my( $worst ) = map { $_->[0] } max_by { $_->[1] } pairs %nappers;
#p $sleep{$worst};
say "worst offender: $worst";
my @x = pairs pairmap { $a => sum map { $_->[1] - $_->[0] } @$b } %sleep;
# p @x;
my %times = pairmap { $a => scalar @$b } partition_by { $_ } map { $_->[0]..$_->[1]-1 } $sleep{$worst}->@*;
p %times;
my ($minute) = map { $_->[0] } max_by { $_->[1] } pairs %times;
say $minute;
say "$worst, $minute ", $guard * $minute;
say $worst * $minute;
# 51618 is too low

17
2018/04/test Normal file
View File

@ -0,0 +1,17 @@
[1518-11-01 00:00] Guard #10 begins shift
[1518-11-01 00:05] falls asleep
[1518-11-01 00:25] wakes up
[1518-11-01 00:30] falls asleep
[1518-11-01 00:55] wakes up
[1518-11-01 23:58] Guard #99 begins shift
[1518-11-02 00:40] falls asleep
[1518-11-02 00:50] wakes up
[1518-11-03 00:05] Guard #10 begins shift
[1518-11-03 00:24] falls asleep
[1518-11-03 00:29] wakes up
[1518-11-04 00:02] Guard #99 begins shift
[1518-11-04 00:36] falls asleep
[1518-11-04 00:46] wakes up
[1518-11-05 00:03] Guard #99 begins shift
[1518-11-05 00:45] falls asleep
[1518-11-05 00:55] wakes up