]> www.fi.muni.cz Git - aoc.git/blob - 2018/07.pl
Year 2018
[aoc.git] / 2018 / 07.pl
1 #!/usr/bin/perl -w
2
3 use v5.30;
4 use strict;
5
6 my ($g, $sleep_m);
7 my (%sleep_g, %sleep_g_m);
8 for (sort <>) {
9         if (/#(\d+) begins/) {
10                 $g = $1;
11         } elsif (/:(\d+)\] falls/) {
12                 $sleep_m = $1;
13         } elsif (/:(\d+)\] wakes/) {
14                 $sleep_g{$g} += $1 - $sleep_m;
15                 for my $min ($sleep_m .. $1 - 1) {
16                         $sleep_g_m{$g}{$min}++;
17                 }
18         }
19 }
20
21 my $max_g;
22 my $max_g_sleep;
23 for my $g (keys %sleep_g) {
24         if (!$max_g_sleep || $max_g_sleep < $sleep_g{$g}) {
25                 $max_g = $g;
26                 $max_g_sleep = $sleep_g{$g};
27         }
28 }
29 my $max_min;
30 my $max_min_sleep;
31 for my $min (keys %{ $sleep_g_m{$max_g} }) {
32         my $n = $sleep_g_m{$max_g}{$min};
33         if (!defined $max_min_sleep || $max_min_sleep < $n) {
34                 $max_min = $min;
35                 $max_min_sleep = $n;
36         }
37 }
38
39 say $max_g * $max_min;
40                         
41                         
42
43                 
44         
45