From 692e0301e72b867d8a48319859af5d3b5052b440 Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Fri, 8 Dec 2023 10:52:54 -0500 Subject: [PATCH] part 1 wip --- 2023/08/Part1.pm | 2 +- 2023/08/Part2.pm | 26 ++++++++++++++++++++++++-- 2023/08/part1.t | 5 ++--- 2023/08/part2.t | 18 +++++++++++++++++- 4 files changed, 44 insertions(+), 7 deletions(-) diff --git a/2023/08/Part1.pm b/2023/08/Part1.pm index 6b9823b..8b68061 100644 --- a/2023/08/Part1.pm +++ b/2023/08/Part1.pm @@ -12,7 +12,7 @@ sub parse_input ($input) { my %nodes; for (@nodes) { - my @n = /([A-Z]+)/g; + my @n = /([A-Z\d]+)/g; $nodes{ shift @n } = \@n; } diff --git a/2023/08/Part2.pm b/2023/08/Part2.pm index 6e5705f..d7a70ee 100644 --- a/2023/08/Part2.pm +++ b/2023/08/Part2.pm @@ -4,10 +4,32 @@ package Part2; use Part1; -use List::AllUtils qw/ /; +use List::AllUtils qw/all /; sub solution_2 ($input) { - ...; + my $p = Part1::parse_input($input); + my @directions = $p->{directions}->@*; + my %nodes = $p->{nodes}->%*; + + my $visited = 0; + my $next_index = 0; + my @current = grep { /A$/ } keys %nodes; + + until ( all { /Z$/ } @current ) { + # use DDP; p @current; + # my $foo = <>; + $visited++; + + @current = map { + $nodes{$_}->[ $directions[ $next_index % @directions ] ] + } @current; + $next_index++; + } + + return $visited; + } 1; + +1; diff --git a/2023/08/part1.t b/2023/08/part1.t index 8661603..2aa0884 100644 --- a/2023/08/part1.t +++ b/2023/08/part1.t @@ -6,6 +6,7 @@ use Path::Tiny; use File::Serialize; use Part1; +use Part2; my $input = path('input')->slurp; my $example = path('example')->slurp; @@ -13,9 +14,6 @@ my %solutions = deserialize_file('solutions.yml')->%*; my $parsed = Part1::parse_input($example); -use DDP; -p $parsed; - is $parsed->{directions} => [ 1, 0 ]; is $parsed->{nodes}{AAA} => [qw/ BBB CCC /]; @@ -33,4 +31,5 @@ SKIP: { is Part1::solution_1($input) => $solutions{1}; } + done_testing; diff --git a/2023/08/part2.t b/2023/08/part2.t index 0401ccc..cb5f141 100644 --- a/2023/08/part2.t +++ b/2023/08/part2.t @@ -3,11 +3,27 @@ use 5.38.0; use Test2::V0; use Path::Tiny; +use File::Serialize; use Part2; my $input = path('input')->slurp; +my $example = path('example')->slurp; +my %solutions = deserialize_file('solutions.yml')->%*; -is Part2::solution_2($input) => 'TODO'; +is Part2::solution_2(<<'END') => 6; +LR + +11A = (11B, XXX) +11B = (XXX, 11Z) +11Z = (11B, XXX) +22A = (22B, XXX) +22B = (22C, 22C) +22C = (22Z, 22Z) +22Z = (22B, 22B) +XXX = (XXX, XXX) +END + +is Part2::solution_2($input) => $solutions{2}; done_testing;