-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path12.pl
73 lines (64 loc) · 1.42 KB
/
12.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
73
#!/usr/bin/perl -w
use strict;
no warnings 'portable';
use Data::Dumper;
use feature 'say';
use Clipboard;
use List::Util qw/sum min max reduce any all none notall first product uniq pairs mesh zip/;
use Math::Cartesian::Product;
use Math::Complex;
use List::PriorityQueue;
use Memoize;
use Term::ANSIColor qw(:constants);
use Storable qw(dclone);
# use POSIX;
sub out {
my $out = shift;
if (ref($out)) {
print Dumper($out);
} else {
Clipboard->copy_to_all_selections("./submit.py $out");
print "$out\n";
}
}
sub solve {
no warnings 'recursion';
my $str = shift;
my $sum = 0;
#say "SOLVE($str,".join(',',@v).")";
# If no more values, check if remainder can all be .
if (!@_) {
my $ret = $str =~ /^[\.\?]*$/o;
#say "no more v - returning $ret";
return $ret;
}
# If the first char can be '.', try that option
if ($str =~ /^[\.\?](.*)$/o) {
#say "assuming .";
$sum += solve($1,@_);
}
# If the next series of # can start here, try that option
my $v0 = shift;
if ($str =~ /^[\#\?]{$v0}(?:[\.\?](.*))?$/) {
#say "placing series of #";
$sum += solve($1||'',@_);
}
#say "returning $sum";
return $sum;
}
memoize('solve');
my $sumA = 0;
my $sumB = 0;
while (<>) {
chomp;
last unless $_;
m{([#\?\.]+) ([\d,]+)}o;
my $r = $1;
my @v = split(',',$2);
$sumA += solve($r,@v);
$r = join('?', ($r) x 5);
@v = (@v) x 5;
$sumB += solve($r,@v);
}
out ($sumA);
out ($sumB);