]> www.fi.muni.cz Git - aoc.git/blob - 2018/08.pl
Day 25: examining the input
[aoc.git] / 2018 / 08.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_min;
23 my $max_min_sleep;
24 for my $g (keys %sleep_g) {
25         for my $min (keys %{ $sleep_g_m{$g} }) {
26                 my $n = $sleep_g_m{$g}{$min};
27                 if (!defined $max_min_sleep || $max_min_sleep < $n) {
28                         $max_g = $g;
29                         $max_min = $min;
30                         $max_min_sleep = $n;
31                 }
32         }
33 }
34
35 say $max_g * $max_min;
36                         
37                         
38
39                 
40         
41