-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path24.pl
217 lines (203 loc) · 4.78 KB
/
24.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#!/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/;
use Math::Cartesian::Product;
use Math::Complex;
use List::PriorityQueue;
use Memoize;
sub out {
my $out = shift;
if (ref($out)) {
print Dumper($out);
} else {
Clipboard->copy_to_all_selections($out);
print "$out\n";
}
}
# Utility function to make sure 2d array has equal rows
# input arr,fill. no output
sub equalize {
my $arr = shift;
my $fill = shift;
my $rows = @$arr;
my $cols = max(map {$_ ? scalar(@$_): 0} @$arr);
print "$rows $cols\n";
for my $row (@$arr) {
$row = [] unless $row;
while (@$row < $cols) {
push @$row, $fill;
}
}
}
# Find neighbors
# input neigh_arr, arr, row, col
# OR: neigh_arr, arr, "row,col"
# OR: neigh_arr, rows, cols, row, col
# OR: neigh_arr, rows, cols, "row,col"
# returns array of [row, col, value]
# OR: array of ["row,col", value]
# OR: array of "row,col"
sub neigh {
my $neigh = shift;
my ($rows,$cols);
my $arr = shift;
if (ref $arr) {
$rows = @$arr;
$cols = @{$arr->[0]};
} else {
$rows = $arr;
$cols = shift;
undef $arr;
}
my $row = shift;
my $col = shift;
my $comma;
if ($row =~ /(\d+)(\D+)(\d+)/) {
($row, $comma, $col) = ($1, $2, $3);
}
my @out;
for my $pair (@$neigh) {
my ($rd, $cd) = @$pair;
my ($nr, $nc) = ($row + $rd, $col + $cd);
next if $nr < 0;
next if $nc < 0;
next if $nr >= $rows;
next if $nc >= $cols;
if (defined($comma)) {
if ($arr) {
push @out, ["$nr$comma$nc", $arr->[$nr][$nc]];
} else {
push @out, "$nr$comma$nc";
}
} else {
push @out, [$nr, $nc, $arr ? ($arr->[$nr][$nc],) : ()];
}
}
return @out;
}
# Orthogonal
sub oneigh {
return neigh([[-1,0], [1, 0], [0, -1], [0, 1]], @_);
}
# All neighbors
sub aneigh {
return neigh([
[-1, -1], [-1, 0], [-1, 1],
[ 0, -1], [ 0, 1],
[ 1, -1], [ 1, 0], [ 1, 1]], @_);
}
# Numeric sort because sort defaults to lex
# returns new array
sub nsort {
my $in = \@_;
if (@$in == 1) {
$in = $in->[0];
}
return sort {$a <=> $b} @$in;
}
# Binary conversions
sub bin2dec {
my $in = shift;
return oct("0b$in");
}
sub dec2bin {
my $in = shift;
return sprintf ("%b", $in);
}
sub hex2bin {
my $in = shift;
return join('', map {sprintf("%04b", oct("0x$_"))} split('', $in));
}
sub bin2hex {
my $in = shift;
my $out;
my @in = split('', $in);
die "Bad bin value $in" if (@in%4);
while (@in) {
$out .= sprintf("%X", bin2dec(join('',splice(@in, 0, 4))));
}
return $out;
}
# A* / BFS implementation
# Args: start, end, neighbor function, heuristic function
# neighbor function: node -> [[new_node, cost], ...]
# cost assumed 1 if missing
# heuristic function: node -> lower bound on cost to end
sub astar {
my ($start, $end, $neigh, $h) = @_;
$h = sub {return 0;} unless $h;
my $OPEN = new List::PriorityQueue;
my %gscore = ($start, 0);
my %OHASH = ($start, 1);
my %path;
$OPEN->insert($start, $h->($start));
while (%OHASH) {
my $cur = $OPEN->pop();
delete $OHASH{$cur};
if ($cur eq $end) {
my $score = $gscore{$cur};
return $score unless wantarray;
my @path = ($cur);
while ($cur = $path{$cur}) {
unshift(@path, $cur)
}
return ($score, @path);
}
for my $n ($neigh->($cur)) {
my ($np,$v) = @$n;
if (!defined($v)) {
$v = 1;
}
my $new_g = $gscore{$cur} + $v;
if (!exists($gscore{$np}) || $new_g < $gscore{$np}) {
$path{$np} = $cur if wantarray;
$gscore{$np} = $new_g;
my $fscore = $new_g + $h->($np);
if (!$OHASH{$np}) {
$OPEN->insert($np, $fscore);
$OHASH{$np}++;
} else {
$OPEN->update($np, $fscore);
}
}
}
}
}
my @A;
my %H;
my $sum=0;
while (<>) {
chomp;
last unless $_;
push @A, [split(' '), ''];
}
my @input = (1..14);
my %VARS = (qw/w 0 x 0 y 0 z 0/);
for my $l (@A) {
my ($opcode, $acc, $oth) = @$l;
$oth = $VARS{$oth} // $oth;
if ($opcode eq 'inp') {
$VARS{$acc} = ('$x'.(shift @input));
} elsif ($opcode eq 'add') {
$VARS{$acc} = ($VARS{$acc} eq '0' ? $oth : (
$oth eq '0' ? $VARS{$acc} :
'(' . $VARS{$acc}.' + '.$oth . ')'
));
} elsif ($opcode eq 'mul') {
$VARS{$acc} = ($VARS{$acc} eq '0' ? 0: (
$oth eq '0' ? 0 :
'(' . $VARS{$acc}.' * '.$oth . ')'
));
} elsif ($opcode eq 'div') {
$VARS{$acc} = $VARS{$acc} eq '0' ? 0 : 'int(' . $VARS{$acc}.' / '.$oth . ')';
} elsif ($opcode eq 'mod') {
$VARS{$acc} = $VARS{$acc} eq '0' ? 0 : '(' . $VARS{$acc}.' % '.$oth . ')';
} elsif ($opcode eq 'eql') {
$VARS{$acc} = '(' . $VARS{$acc}.' == '.$oth . ')';
}
}
say($VARS{'z'});