part 2
This commit is contained in:
parent
d1d38ba623
commit
d6ed288ea9
@ -4,7 +4,9 @@ package Part2;
|
|||||||
|
|
||||||
use Part1;
|
use Part1;
|
||||||
|
|
||||||
use List::AllUtils qw/all first_index none min max /;
|
use List::AllUtils qw/all first_index none min max product /;
|
||||||
|
use List::UtilsBy qw/ min_by max_by /;
|
||||||
|
use Math::Utils qw/ lcm /;
|
||||||
|
|
||||||
sub get_cycle( $current, $directions, $nodes ) {
|
sub get_cycle( $current, $directions, $nodes ) {
|
||||||
|
|
||||||
@ -12,7 +14,6 @@ sub get_cycle( $current, $directions, $nodes ) {
|
|||||||
my $next_index = 0;
|
my $next_index = 0;
|
||||||
|
|
||||||
while() {
|
while() {
|
||||||
use DDP; #p @tracks;
|
|
||||||
my $t = join '-', $current,$next_index;
|
my $t = join '-', $current,$next_index;
|
||||||
my $i = first_index { $_ eq $t } @tracks;
|
my $i = first_index { $_ eq $t } @tracks;
|
||||||
if( $i > -1 ) {
|
if( $i > -1 ) {
|
||||||
@ -60,6 +61,37 @@ sub speedrun($current,$next_index,$directions,$nodes,$n) {
|
|||||||
return $current;
|
return $current;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sub equation_for($current,$directions,$nodes) {
|
||||||
|
|
||||||
|
my $visited = 0;
|
||||||
|
my $next_index=0;
|
||||||
|
|
||||||
|
while ( ) {
|
||||||
|
$visited++;
|
||||||
|
|
||||||
|
$current =
|
||||||
|
$nodes->{$current}[ $directions->[ $next_index++ % @$directions ] ];
|
||||||
|
|
||||||
|
last if $current =~ /Z$/
|
||||||
|
}
|
||||||
|
|
||||||
|
my $first = $visited;
|
||||||
|
$visited = 0;
|
||||||
|
|
||||||
|
while ( ) {
|
||||||
|
$visited++;
|
||||||
|
|
||||||
|
$current =
|
||||||
|
$nodes->{$current}[ $directions->[ $next_index++ % @$directions ] ];
|
||||||
|
|
||||||
|
last if $current =~ /Z$/
|
||||||
|
}
|
||||||
|
|
||||||
|
my $entry = { b => $first, m => $visited };
|
||||||
|
return $entry;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
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}->@*;
|
||||||
@ -69,20 +101,27 @@ sub solution_2 ($input) {
|
|||||||
my @paths = grep { /A$/ } keys %nodes;
|
my @paths = grep { /A$/ } keys %nodes;
|
||||||
my $next_index = 0;
|
my $next_index = 0;
|
||||||
|
|
||||||
until( all {/Z$/ } @paths ) {
|
my @equations = map { equation_for($_,\@directions,\%nodes) } @paths;
|
||||||
my($incr,$current,$new_index) = find_z($paths[0],$next_index,\@directions,\%nodes);
|
|
||||||
$paths[0] = $current;
|
return lcm map { $_->{b} } @equations;
|
||||||
@paths = ( $current, map {
|
|
||||||
speedrun($paths[$_],$next_index,\@directions,\%nodes,$incr)
|
for(@equations) {
|
||||||
} 1..$#paths );
|
$_->{i} = 1;
|
||||||
$next_index = $new_index;
|
$_->{total} = $_->{b}+$_->{m};
|
||||||
$steps += $incr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
until( all { $_->{total} == $equations[0]->{total} } @equations ) {
|
||||||
|
my $i = max map { $_->{total} } @equations;
|
||||||
|
|
||||||
return $steps;
|
for(@equations) {
|
||||||
|
while( $_->{total}<$i) {
|
||||||
|
$_->{total}+=$_->{m};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
# use DDP; p @equations;
|
||||||
|
}
|
||||||
|
|
||||||
|
$equations[0]->{total};
|
||||||
}
|
}
|
||||||
|
|
||||||
1;
|
1;
|
||||||
|
@ -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;
|
||||||
|
@ -29,7 +29,7 @@ my $parsed = Part1::parse_input($example);
|
|||||||
#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;
|
||||||
|
|
||||||
$parsed = Part1::parse_input($input);
|
$parsed = Part1::parse_input($input);
|
||||||
|
|
||||||
|
@ -1,2 +1,2 @@
|
|||||||
1: 19631
|
1: 19631
|
||||||
2: TODO
|
2: 21003205388413
|
||||||
|
@ -18,3 +18,6 @@
|
|||||||
{"day":"7","part":2,"persec":85.2380952380952,"year":"2023","timestamp":"2023-12-07T16:08:43","time":0.011731843575419,"language":"perl"}
|
{"day":"7","part":2,"persec":85.2380952380952,"year":"2023","timestamp":"2023-12-07T16:08:43","time":0.011731843575419,"language":"perl"}
|
||||||
{"persec":113.488372093023,"timestamp":"2023-12-09T17:57:35","language":"perl","year":"2023","time":0.00881147540983607,"day":"9","part":1}
|
{"persec":113.488372093023,"timestamp":"2023-12-09T17:57:35","language":"perl","year":"2023","time":0.00881147540983607,"day":"9","part":1}
|
||||||
{"persec":109.783631232361,"timestamp":"2023-12-09T17:57:47","language":"perl","time":0.00910882604970008,"day":"9","part":2,"year":"2023"}
|
{"persec":109.783631232361,"timestamp":"2023-12-09T17:57:47","language":"perl","time":0.00910882604970008,"day":"9","part":2,"year":"2023"}
|
||||||
|
{"day":"8","persec":131.374407582938,"language":"perl","part":1,"time":0.00761183261183261,"timestamp":"2023-12-09T19:45:23","year":"2023"}
|
||||||
|
{"day":"8","language":"perl","time":0.0874789915966387,"year":"2023","timestamp":"2023-12-09T20:31:05","persec":11.431316042267,"part":2}
|
||||||
|
|
||||||
|
1
2023/cpanfile
Normal file
1
2023/cpanfile
Normal file
@ -0,0 +1 @@
|
|||||||
|
requires 'Math::Utils';
|
Loading…
Reference in New Issue
Block a user