-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday-4-star-1.pl
executable file
·39 lines (31 loc) · 961 Bytes
/
day-4-star-1.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
#!/usr/bin/perl
use warnings;
use 5.036;
chomp(my @lines = <>);
my $sum = 0;
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;
# count points per card
for my $number (@myNumbers) {
if (exists $winNumbers{$number}) {
push(@numbersMatched, $number);
if ($cardValue == 0) {
$cardValue = 1;
} else {
$cardValue *= 2;
}
}
}
# say $card . ": " . $cardValue . " " . "@numbersMatched";
# say $card . " win numbers: @winNumbers" . " value: " . $cardValue;
$sum += $cardValue;
}
say $sum;