summary refs log tree commit diff homepage
path: root/2021/day06.pl
blob: 6e5fa15c3f5b690e24ece4d34de747aad0f0a1ba (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
use strict;
use warnings;
my @init = split(/,/, <>);
my @fish = @init;
for (1..80) {
	my @spawns;
	for (@fish) {
		if ($_) {
			$_--;
		} else {
			$_ = 6;
			push @spawns, 8;
		}
	}
	push @fish, @spawns;
}
print scalar(@fish), "\n";
my %counts;
$counts{$_}++ for @init;
for (1..256) {
	my $zeros = $counts{0} // 0;
	$counts{$_} = $counts{$_+1} for 0..7;
	$counts{8} = $zeros;
	$counts{6} += $zeros;
}
my $sum = 0;
$sum += $_ for values %counts;
print "$sum\n";