diff options
Diffstat (limited to '')
-rw-r--r-- | 2021/day06.pl | 26 |
1 files changed, 21 insertions, 5 deletions
diff --git a/2021/day06.pl b/2021/day06.pl index b296a4c..3fc7ac2 100644 --- a/2021/day06.pl +++ b/2021/day06.pl @@ -1,16 +1,32 @@ use strict; use warnings; -my @fish = split(/,/, <>); +my @init = split(/,/, <>); +my @fish = @init; for (1..80) { my @spawns; - for my $f (@fish) { - if ($f) { - $f--; + for (@fish) { + if ($_) { + $_--; } else { - $f = 6; + $_ = 6; push @spawns, 8; } } push @fish, @spawns; } print scalar(@fish), "\n"; +my %counts; +for (@init) { + $counts{$_}++; +} +for (1..256) { + my $zeros = $counts{0} // 0; + for (0..7) { + $counts{$_} = $counts{$_+1}; + } + $counts{8} = $zeros; + $counts{6} += $zeros; +} +my $sum = 0; +$sum += $_ for values %counts; +print "$sum\n"; |