wip
This commit is contained in:
parent
450ac404f1
commit
d1d38ba623
@ -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) {
|
||||
my $p = Part1::parse_input($input);
|
||||
my @directions = $p->{directions}->@*;
|
||||
my %nodes = $p->{nodes}->%*;
|
||||
|
||||
my @paths;
|
||||
my @initial = grep { /A$/ } keys %nodes;
|
||||
for (@initial ) {
|
||||
push @paths, [
|
||||
Part2::get_cycle($_,\@directions,\%nodes)
|
||||
];
|
||||
my $steps = 0;
|
||||
my @paths = grep { /A$/ } keys %nodes;
|
||||
my $next_index = 0;
|
||||
|
||||
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;
|
||||
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;
|
||||
return $steps;
|
||||
|
||||
|
||||
}
|
||||
|
||||
1;
|
||||
|
||||
1;
|
||||
|
@ -15,7 +15,7 @@ my $solutions = deserialize_file('solutions.yml');
|
||||
|
||||
my @parts = (
|
||||
{ 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;
|
||||
@ -36,6 +36,7 @@ for my $part (@parts) {
|
||||
language => 'perl',
|
||||
part => $part->{part},
|
||||
time => $res->cpu_a / $res->iters,
|
||||
persec => $res->iters / $res->cpu_a,
|
||||
timestamp => DateTime->now->iso8601,
|
||||
};
|
||||
say to_json $result;
|
||||
|
@ -24,15 +24,12 @@ my %solutions = deserialize_file('solutions.yml')->%*;
|
||||
|
||||
|
||||
my $parsed = Part1::parse_input($example);
|
||||
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;
|
||||
is [Part2::get_cycle('22A',$parsed->{directions},$parsed->{nodes})] => [1,2,3];
|
||||
#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;
|
||||
#is [Part2::get_cycle('22A',$parsed->{directions},$parsed->{nodes})] => [1,2,3];
|
||||
|
||||
|
||||
is Part2::solution_2($example) => 6;
|
||||
exit;
|
||||
|
||||
|
||||
|
||||
$parsed = Part1::parse_input($input);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user