]> www.fi.muni.cz Git - aoc.git/blobdiff - 2017/11.pl
AoC 2017 begins
[aoc.git] / 2017 / 11.pl
diff --git a/2017/11.pl b/2017/11.pl
new file mode 100755 (executable)
index 0000000..1668038
--- /dev/null
@@ -0,0 +1,27 @@
+#!/usr/bin/perl
+
+use v5.30;
+use strict;
+
+chomp (my @banks = split /\s+/, <>);
+
+my %seen;
+my $steps;
+while (1) {
+       my $key = join(',', @banks);
+       last if $seen{$key};
+       $steps++;
+       my ($max, $maxi);
+       for my $i (0 .. $#banks) {
+               if ($banks[$i] > $max) {
+                       $max = $banks[$i];
+                       $maxi = $i;
+               }
+       }
+       $banks[$maxi] = 0;
+       while ($max--) {
+               $maxi = 0 if ++$maxi > $#banks;
+               $banks[$maxi]++;
+       }
+}
+say $steps;