diff options
author | June McEnroe <june@causal.agency> | 2021-12-06 10:42:57 -0500 |
---|---|---|
committer | June McEnroe <june@causal.agency> | 2021-12-06 10:42:57 -0500 |
commit | 2145fe3dc17910991e5acbe1bdc8d1d8ec310e78 (patch) | |
tree | 270ada8595b2546afd948b707fa5768584c2db6a /2021 | |
parent | Solve day 6 part 1 (diff) | |
download | aoc-2145fe3dc17910991e5acbe1bdc8d1d8ec310e78.tar.gz aoc-2145fe3dc17910991e5acbe1bdc8d1d8ec310e78.zip |
Solve day 6 part 2
Diffstat (limited to '2021')
-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"; |