adventofcode/2020/15/part2.pm

45 lines
732 B
Perl
Raw Normal View History

2021-12-05 16:51:21 +00:00
package part2;
use 5.20.0;
use warnings;
use List::AllUtils qw/ /;
require './part1.pm';
use experimental qw/ signatures postderef /;
sub solution(@seeds) {
my $last = shift @seeds;
my $turn = 1;
my %said;
for(@seeds) {
$said{$last} ||= [];
push $said{$last}->@*, $turn++;
$last = $_;
}
until( $turn == 30000000 ) {
if( !$said{$last} ) {
$said{$last} = [$turn];
$last = 0
} else {
push $said{$last}->@*, $turn;
if( $said{$last}->@* > 2 ) {
shift $said{$last}->@*;
}
$last = $said{$last}[-1] - $said{$last}[-2];
}
$turn++;
}
return $last;
}
1;