wip
This commit is contained in:
parent
9856545a59
commit
450ac404f1
@ -4,30 +4,86 @@ package Part2;
|
|||||||
|
|
||||||
use Part1;
|
use Part1;
|
||||||
|
|
||||||
use List::AllUtils qw/all /;
|
use List::AllUtils qw/all first_index none min max /;
|
||||||
|
|
||||||
|
sub get_cycle( $current, $directions, $nodes ) {
|
||||||
|
|
||||||
|
my @tracks;
|
||||||
|
my $next_index = 0;
|
||||||
|
|
||||||
|
while() {
|
||||||
|
use DDP; #p @tracks;
|
||||||
|
my $t = join '-', $current,$next_index;
|
||||||
|
my $i = first_index { $_ eq $t } @tracks;
|
||||||
|
if( $i > -1 ) {
|
||||||
|
my $c = 0;
|
||||||
|
# p @tracks;
|
||||||
|
# warn $i, " ",$t;
|
||||||
|
return $i, map {
|
||||||
|
$c++;
|
||||||
|
if(/Z-/) {
|
||||||
|
my $x = $c; $c = 0; $x;
|
||||||
|
}else {
|
||||||
|
()
|
||||||
|
}
|
||||||
|
} splice @tracks, $i;
|
||||||
|
}
|
||||||
|
push @tracks, $t;
|
||||||
|
$current = $nodes->{$current}[ $directions->[ $next_index ] ];
|
||||||
|
$next_index = ( $next_index+1)% @$directions;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
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 @initial = grep { /A$/ } keys %nodes;
|
||||||
|
for (@initial ) {
|
||||||
|
push @paths, [
|
||||||
|
Part2::get_cycle($_,\@directions,\%nodes)
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
my $visited = 0;
|
my $visited = 0;
|
||||||
my $next_index = 0;
|
my @preloop = map { shift @$_ } @paths;
|
||||||
my @current = grep { /A$/ } keys %nodes;
|
p @preloop;
|
||||||
|
my $pre = max @preloop;
|
||||||
|
$visited += $pre;
|
||||||
|
|
||||||
until ( all { /Z$/ } @current ) {
|
my @current = map { $preloop[$_] - $pre } 0..$#preloop;
|
||||||
# use DDP; p @current;
|
p @current;p @paths;
|
||||||
# my $foo = <>;
|
for(0..$#current) {
|
||||||
$visited++;
|
while( $current[$_] <= 0 ) {
|
||||||
|
$current[$_] += $paths[$_][0];
|
||||||
|
push $paths[$_]->@*, shift $paths[$_]->@*;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
p @current;p @paths;
|
||||||
|
|
||||||
@current = map {
|
until( none { $_ } @current ) {
|
||||||
$nodes{$_}->[ $directions[ $next_index % @directions ] ]
|
use DDP;
|
||||||
} @current;
|
warn $visited;
|
||||||
$next_index++;
|
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 $visited;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
1;
|
1;
|
||||||
|
@ -8,10 +8,7 @@ use File::Serialize;
|
|||||||
use Part2;
|
use Part2;
|
||||||
|
|
||||||
my $input = path('input')->slurp;
|
my $input = path('input')->slurp;
|
||||||
my $example = path('example')->slurp;
|
my $example = <<END;
|
||||||
my %solutions = deserialize_file('solutions.yml')->%*;
|
|
||||||
|
|
||||||
is Part2::solution_2(<<'END') => 6;
|
|
||||||
LR
|
LR
|
||||||
|
|
||||||
11A = (11B, XXX)
|
11A = (11B, XXX)
|
||||||
@ -23,6 +20,27 @@ LR
|
|||||||
22Z = (22B, 22B)
|
22Z = (22B, 22B)
|
||||||
XXX = (XXX, XXX)
|
XXX = (XXX, XXX)
|
||||||
END
|
END
|
||||||
|
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::solution_2($example) => 6;
|
||||||
|
exit;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
$parsed = Part1::parse_input($input);
|
||||||
|
|
||||||
|
# my @initial = grep { /A$/ } keys $parsed->{nodes}->%*;
|
||||||
|
# for (@initial ) {
|
||||||
|
# say join " ", Part2::get_cycle($_,$parsed->{directions},$parsed->{nodes});
|
||||||
|
# }
|
||||||
|
|
||||||
|
|
||||||
is Part2::solution_2($input) => $solutions{2};
|
is Part2::solution_2($input) => $solutions{2};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user