]> www.fi.muni.cz Git - aoc.git/blob - 2017/12.pl
Day 25: examining the input
[aoc.git] / 2017 / 12.pl
1 #!/usr/bin/perl
2
3 use v5.30;
4 use strict;
5
6 chomp (my @banks = split /\s+/, <>);
7
8 my %seen;
9 my $steps;
10 my $another;
11 while (1) {
12         my $key = join(',', @banks);
13         last if defined $another && $another eq $key;
14         if ($seen{$key}++ && !defined $another) {
15                 $another = $key;
16                 $steps = 0;
17         }
18         $steps++;
19         my ($max, $maxi);
20         for my $i (0 .. $#banks) {
21                 if ($banks[$i] > $max) {
22                         $max = $banks[$i];
23                         $maxi = $i;
24                 }
25         }
26         $banks[$maxi] = 0;
27         while ($max--) {
28                 $maxi = 0 if ++$maxi > $#banks;
29                 $banks[$maxi]++;
30         }
31 }
32 say $steps;