-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path15-b.ldpl
300 lines (283 loc) · 7.53 KB
/
15-b.ldpl
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
using package std-list
data:
auxText is text
programList is text list
auxNumber is number
program is text map
pc is text
inputBuffer is text list
outputBuffer is text list
relBase is text
explored is text map map
deltas is number list list
used is number
size is number
programs is text map list
pcs is text list
relBases is text list
i is number
steps is number
stepss is number list
x is number
y is number
xs is number list
ys is number list
procedure:
load file "15.txt" in auxText
split auxText by "," in programList
store 0 in auxNumber
for each auxText in programList do
trim auxText in auxText
store auxText in program:auxNumber
in auxNumber solve auxNumber + 1
repeat
push list to deltas
push list to deltas
push list to deltas
push list to deltas
push list to deltas
push 0 to deltas:1
push -1 to deltas:1
push 0 to deltas:2
push 1 to deltas:2
push -1 to deltas:3
push 0 to deltas:3
push 1 to deltas:4
push 0 to deltas:4
push map to programs
copy program to programs:0
push "0" to pcs
push "0" to relBases
push 0 to xs
push 0 to ys
push 0 to stepss
while 0 is equal to 0 do
in steps solve steps + 1
for i from 1 to 5 step 1 do
copy programs:used to program
store pcs:used in pc
store relBases:used in relBase
store xs:used in x
store ys:used in y
in steps solve stepss:used + 1
in x solve x + deltas:i:0
in y solve y + deltas:i:1
if explored:x:y is equal to "1" then
continue
end if
store "1" in explored:x:y
store i in auxText
clear inputBuffer
push auxText to inputBuffer
clear outputBuffer
call runProgram with program pc inputBuffer outputBuffer 0 relBase 0
if outputBuffer:0 is equal to "2" then
clear programs
clear pcs
clear relBases
clear xs
clear ys
clear stepss
clear explored
store "1" in explored:x:y
push map to programs
store 0 in size
copy program to programs:size
push pc to pcs
push relBase to relBases
push x to xs
push y to ys
push 0 to stepss
store -1 in used
else if outputBuffer:0 is equal to "1" then
push map to programs
in size solve size + 1
copy program to programs:size
push pc to pcs
push relBase to relBases
push x to xs
push y to ys
push steps to stepss
end if
repeat
in used solve used + 1
get length of pcs in auxNumber
if used is greater than or equal to auxNumber then
in steps solve steps - 1
display steps crlf
exit
end if
repeat
sub runProgram
parameters:
program is text map
pc is text
inputBuffer is text list
outputBuffer is text list
inputCount is number
relBase is text
haltSignal is number
local data:
opcode is text
mode0 is text
mode1 is text
mode2 is text
inA is text
inB is text
out is text
auxNumber is number
procedure:
while 0 is equal to 0 do
store program:pc in opcode
if opcode is equal to "" then
store "0" in opcode
end if
call sumBig with pc "1" pc
while 0 is equal to 0 do # Pad with 0
get length of opcode in auxNumber
if auxNumber is less than 5 then
in opcode join "0" opcode
else
break
end if
repeat
get character at 0 from opcode in mode2
get character at 1 from opcode in mode1
get character at 2 from opcode in mode0
substring opcode from 3 length 2 in opcode
if opcode is equal to "99" then
store 1 in haltSignal
return
else if opcode is not equal to "03" then
call readParameter with mode0 program pc relBase inA
call sumBig with pc "1" pc
if opcode is equal to "04" then
push inA to outputBuffer
else if opcode is equal to "09" then
call sumBig with relBase inA relBase
else
call readParameter with mode1 program pc relBase inB
call sumBig with pc "1" pc
if opcode is equal to "01" then
call sumBig with inA inB out
else if opcode is equal to "02" then
call mulBig with inA inB out
else if opcode is equal to "05" then
if inA is not equal to "0" then
store inB in pc
end if
continue
else if opcode is equal to "06" then
if inA is equal to "0" then
store inB in pc
end if
continue
else if opcode is equal to "07" then
call lessBig with inA inB out
else if opcode is equal to "08" then
if inA is equal to inB then
store 1 in out
else
store 0 in out
end if
end if
if program:pc is equal to "" then
store 0 in program:pc
end if
call saveParameter with mode2 program pc relBase out
call sumBig with pc "1" pc
end if
else # 03
get length of inputBuffer in auxNumber
if inputCount is less than auxNumber then
if program:pc is equal to "" then
store 0 in program:pc
end if
call saveParameter with mode0 program pc relBase inputBuffer:inputCount
call sumBig with pc "1" pc
in inputCount solve inputCount + 1
else
call sumBig with pc "-1" pc
return
end if
end if
repeat
end sub
sub readParameter
parameters:
mode is text
program is text map
pc is text
relBase is text
result is text
procedure:
if mode is equal to "0" then
call readParameter with "2" program pc "0" result
else if mode is equal to "1" then
store program:pc in result
else
call readParameter with "1" program pc relBase result
call sumBig with result relBase result
store program:result in result
end if
if result is equal to "" then
store "0" in result
end if
end sub
sub saveParameter
parameters:
mode is text
program is text map
pc is text
relBase is text
save is text
local data:
result is text
procedure:
if mode is equal to "0" then
call saveParameter with "2" program pc "0" save
else
call readParameter with "1" program pc relBase result
call sumBig with result relBase result
store save in program:result
end if
end sub
sub sumBig
parameters:
a is text
b is text
res is text
local data:
resNum is number
procedure:
in resNum solve a + b
store resNum in res
end sub
sub mulBig
parameters:
a is text
b is text
res is text
local data:
resNum is number
procedure:
in resNum solve a * b
store resNum in res
end sub
sub lessBig
parameters:
a is text
b is text
res is text
local data:
aNum is number
bNum is number
procedure:
store a in aNum
store b in bNum
if aNum is less than bNum then
store "1" in res
else
store "0" in res
end if
end sub