summary refs log tree commit diff homepage
path: root/2023
diff options
context:
space:
mode:
Diffstat (limited to '')
-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";