This commit is contained in:
Yanick Champoux 2023-12-09 15:05:07 -05:00
parent 450ac404f1
commit d1d38ba623
3 changed files with 42 additions and 47 deletions

View File

@ -36,56 +36,53 @@ sub get_cycle( $current, $directions, $nodes ) {
} }
sub find_z($current,$next_index,$directions,$nodes) {
my $visited = 0;
while ( ) {
$visited++;
$current =
$nodes->{$current}[ $directions->[ $next_index++ % @$directions ] ];
last if $current =~ /Z$/
}
return $visited, $current, $next_index;
}
sub speedrun($current,$next_index,$directions,$nodes,$n) {
$current =
$nodes->{$current}[ $directions->[ $next_index++ % @$directions ] ]
for 1..$n;
return $current;
}
sub solution_2 ($input) { sub solution_2 ($input) {
my $p = Part1::parse_input($input); my $p = Part1::parse_input($input);
my @directions = $p->{directions}->@*; my @directions = $p->{directions}->@*;
my %nodes = $p->{nodes}->%*; my %nodes = $p->{nodes}->%*;
my @paths; my $steps = 0;
my @initial = grep { /A$/ } keys %nodes; my @paths = grep { /A$/ } keys %nodes;
for (@initial ) { my $next_index = 0;
push @paths, [
Part2::get_cycle($_,\@directions,\%nodes) until( all {/Z$/ } @paths ) {
]; my($incr,$current,$new_index) = find_z($paths[0],$next_index,\@directions,\%nodes);
$paths[0] = $current;
@paths = ( $current, map {
speedrun($paths[$_],$next_index,\@directions,\%nodes,$incr)
} 1..$#paths );
$next_index = $new_index;
$steps += $incr;
} }
my $visited = 0;
my @preloop = map { shift @$_ } @paths;
p @preloop;
my $pre = max @preloop;
$visited += $pre;
my @current = map { $preloop[$_] - $pre } 0..$#preloop; return $steps;
p @current;p @paths;
for(0..$#current) {
while( $current[$_] <= 0 ) {
$current[$_] += $paths[$_][0];
push $paths[$_]->@*, shift $paths[$_]->@*;
}
}
p @current;p @paths;
until( none { $_ } @current ) {
use DDP;
warn $visited;
p @current;p @paths;
my $incr = max @current;
warn $incr;
$visited += $incr;
for ( 0..$#current) {
$current[$_] -= $incr;
while( $current[$_] < 0 ) {
$current[$_] += $paths[$_][0];
push $paths[$_]->@*, shift $paths[$_]->@*;
}
}
}
return $visited;
} }
1; 1;
1;

View File

@ -15,7 +15,7 @@ my $solutions = deserialize_file('solutions.yml');
my @parts = ( my @parts = (
{ part => 1, sub => \&Part1::solution_1, expected => $solutions->{1} }, { part => 1, sub => \&Part1::solution_1, expected => $solutions->{1} },
{ part => 2, sub => \&Part2::solution_2, expected => $solutions->{2} }, # { part => 2, sub => \&Part2::solution_2, expected => $solutions->{2} },
); );
my $input = path('./input')->slurp; my $input = path('./input')->slurp;
@ -36,6 +36,7 @@ for my $part (@parts) {
language => 'perl', language => 'perl',
part => $part->{part}, part => $part->{part},
time => $res->cpu_a / $res->iters, time => $res->cpu_a / $res->iters,
persec => $res->iters / $res->cpu_a,
timestamp => DateTime->now->iso8601, timestamp => DateTime->now->iso8601,
}; };
say to_json $result; say to_json $result;

View File

@ -24,15 +24,12 @@ my %solutions = deserialize_file('solutions.yml')->%*;
my $parsed = Part1::parse_input($example); my $parsed = Part1::parse_input($example);
is [Part2::get_cycle('11A',$parsed->{directions},$parsed->{nodes})] => [1,2]; #is [Part2::get_cycle('11A',$parsed->{directions},$parsed->{nodes})] => [1,2];
#use DDP; my @x = Part2::get_cycle('22A',$parsed->{directions},$parsed->{nodes});p @x; ##use DDP; my @x = Part2::get_cycle('22A',$parsed->{directions},$parsed->{nodes});p @x;
is [Part2::get_cycle('22A',$parsed->{directions},$parsed->{nodes})] => [1,2,3]; #is [Part2::get_cycle('22A',$parsed->{directions},$parsed->{nodes})] => [1,2,3];
is Part2::solution_2($example) => 6; is Part2::solution_2($example) => 6;
exit;
$parsed = Part1::parse_input($input); $parsed = Part1::parse_input($input);