summary refs log tree commit diff homepage
path: root/2023/day04.pl
blob: 1cb8ae013daa2931e7362f15b3b05be879386ab1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
use strict;
use warnings;
my $points = 0;
my (%cardWins, %cardNums);
while (<>) {
	chomp;
	$_ =~ 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{$_};
	}
	$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";