commit 7fb1a35db2d56c2368d25abcc8b227c48a357c24
parent a9cb615f58dce1b186048d3379626c10a07754a4
Author: Samir Parikh <noreply@samirparikh.com>
Date: Mon, 17 Oct 2022 17:04:58 +0000
solve part 2 of day03
Diffstat:
1 file changed, 20 insertions(+), 1 deletion(-)
diff --git a/day03/day03.pl b/day03/day03.pl
@@ -23,4 +23,23 @@ foreach ( @directions ) {
}
say "part 1: ", scalar grep { $_ > 0 } values %houses;
-say "part 2: ";
+
+# Solution for Part 2
+undef %houses;
+my ($santa_x, $santa_y, $robot_x, $robot_y) = (0, 0, 0, 0);
+$houses{ 0, 0 } = 2;
+
+for ( my $i = 0; $i < (scalar @directions) - 1; $i += 2 ) {
+ $santa_y += 1 if $directions[ $i ] eq '^';
+ $santa_x += 1 if $directions[ $i ] eq '>';
+ $santa_y -= 1 if $directions[ $i ] eq 'v';
+ $santa_x -= 1 if $directions[ $i ] eq '<';
+ $houses{ $santa_x, $santa_y } += 1;
+ $robot_y += 1 if $directions[ $i + 1 ] eq '^';
+ $robot_x += 1 if $directions[ $i + 1 ] eq '>';
+ $robot_y -= 1 if $directions[ $i + 1 ] eq 'v';
+ $robot_x -= 1 if $directions[ $i + 1 ] eq '<';
+ $houses{ $robot_x, $robot_y } += 1;
+}
+
+say "part 2: ", scalar grep { $_ > 0 } values %houses;