-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path04.pl
72 lines (65 loc) · 1.18 KB
/
04.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/perl -w
use strict;
use Data::Dumper;
use feature 'say';
use Clipboard;
use List::Util qw/sum/;
use Math::Cartesian::Product;
use Math::Complex;
sub out {
my $out = shift;
Clipboard->copy_to_all_selections($out);
print "$out\n";
}
$_=<>;
chomp;
my @numbers = split(/,/);
my @cards;
while (<>) {
my @card;
for my $i (1..5) {
my $line = <>;
chomp $line;
my @row = split(' ', $line);
push @card, \@row;
}
push @cards, \@card;
}
my @winner;
my $winnum;
LOOP: while (@numbers) {
my $n = shift(@numbers);
CARD: for my $card (@cards) {
next unless $card;
for my $row (@$card) {
@$row = map {$_ == $n ? -1 : $_} @$row;
if (sum(@$row) == -5) {
@winner=@$card;
$winnum = $n;
$card = undef;
#last LOOP;
next CARD;
}
}
for my $cn (0..4) {
my $s=0;
for my $rn (0..4) {
$s++ if ($card->[$rn][$cn] == -1);
}
if ($s == 5) {
@winner=@$card;
$winnum = $n;
$card = undef;
#last LOOP;
next CARD;
}
}
}
}
my $sum = 0;
for my $row (@winner) {
for my $n (@$row) {
$sum+=$n if $n >= 0;
}
}
out($sum * $winnum);