-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday-4-star-2.pl
executable file
·49 lines (36 loc) · 1.03 KB
/
day-4-star-2.pl
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/perl
use warnings;
use 5.036;
use List::Util qw(sum);
chomp(my @lines = <>);
my $sum = 0;
my %cardCopies;
my $i = 1;
foreach my $line (@lines) {
my ($card, $winNumbers, $myNumbers) = split(/\s?[\:\|]\s?/, $line);
# get winning numbers
my @winNumbers = split(/\s+/, $winNumbers);
my %winNumbers = map { $_ => 1 } grep { $_ ne '' } @winNumbers;
# get your numbers
my @myNumbers = split(/\s+/, $myNumbers);
@myNumbers = grep { $_ ne '' } @myNumbers;
my $cardValue = 0;
my @numbersMatched;
my $copiesOfCurrentCard = $cardCopies{$i};
# count points per card
for my $number (@myNumbers) {
if (exists $winNumbers{$number}) {
push(@numbersMatched, $number);
}
}
$cardCopies{$i} += 1;
$i++;
for (my $j = 0; $j <= $#numbersMatched; $j++) {
$cardCopies{$i + $j} += $cardCopies{$i - 1};
}
# print "$_ $cardCopies{$_}; " for (sort keys %cardCopies);
# print "\n";
}
my @counts = values %cardCopies;
$sum = sum(@counts);
say $sum;