summary refs log tree commit diff homepage
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2023-12-11 15:58:10 -0500
committerJune McEnroe <june@causal.agency>2023-12-11 15:58:10 -0500
commited637a63e08bd02365d192c570819a2304314c1a (patch)
tree55ff34c5c763f122bc8d1d1b90be5be274646ea0
parentSolve day 4 part 1 (diff)
downloadaoc-ed637a63e08bd02365d192c570819a2304314c1a.tar.gz
aoc-ed637a63e08bd02365d192c570819a2304314c1a.zip
Solve day 4 part 2
-rw-r--r--2023/day04.pl16
1 files changed, 15 insertions, 1 deletions
diff --git a/2023/day04.pl b/2023/day04.pl
index 57b586c..1cb8ae0 100644
--- a/2023/day04.pl
+++ b/2023/day04.pl
@@ -1,12 +1,15 @@
 use strict;
 use warnings;
 my $points = 0;
+my (%cardWins, %cardNums);
 while (<>) {
 	chomp;
-	$_ =~ s/Card (\d+): //;
+	$_ =~ s/Card +(\d+): //;
 	my ($winning, $mine) = split(/ [|] /);
 	my %winning = map {$_ => 1} split(/ +/, $winning);
 	my @mine = split(/ +/, $mine);
+	$cardWins{$1} = \%winning;
+	$cardNums{$1} = \@mine;
 	my $score = 0;
 	for (@mine) {
 		$score = ($score ? $score * 2 : 1) if exists $winning{$_};
@@ -14,3 +17,14 @@ while (<>) {
 	$points += $score;
 }
 print $points, "\n";
+my @cards = keys %cardWins;
+foreach my $card (@cards) {
+	my $wins = 0;
+	for (@{$cardNums{$card}}) {
+		$wins++ if exists $cardWins{$card}->{$_};
+	}
+	for (($card+1)..($card+$wins)) {
+		push @cards, $_ if exists $cardWins{$_};
+	}
+}
+print scalar @cards, "\n";