-
Notifications
You must be signed in to change notification settings - Fork 0
/
21-b.ldpl
219 lines (205 loc) · 5.43 KB
/
21-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
data:
auxText is text
programList is text list
auxNumber is number
program is number list
pc is number
inputBuffer is number list
outputBuffer is number list
inputCount is number
relBase is number
inputText is text
procedure:
load file "21.txt" in auxText
split auxText by "," in programList
for each auxText in programList do
trim auxText in auxText
store auxText in auxNumber
push auxNumber to program
repeat
for auxNumber from 0 to 100000 step 1 do
push 0 to program
repeat
store 0 in pc
store 0 in relBase
store quote in inputText
NOT A J
NOT B T
OR T J
NOT C T
OR T J
AND D J
NOT E T
NOT T T
OR H T
AND T J
RUN
end quote
call inputASCII with inputText inputBuffer
call runProgram with program pc inputBuffer outputBuffer inputCount relBase 0
call outputLast with outputBuffer
sub inputASCII
parameters:
input is text
inputBuffer is number list
local data:
length is number
c is number
char is text
ascii is number
procedure:
get length of input in length
for c from 0 to length step 1 do
get character at c from input in char
get character code of char in ascii
push ascii to inputBuffer
repeat
end sub
sub outputASCII
parameters:
outputBuffer is number list
local data:
ascii is number
char is text
procedure:
for each ascii in outputBuffer do
get ascii character ascii in char
display char
repeat
end sub
sub outputLast
parameters:
outputBuffer is number list
local data:
length is number
procedure:
get length of outputBuffer in length
in length solve length - 1
display outputBuffer:length crlf
end sub
sub runProgram
parameters:
program is number list
pc is number
inputBuffer is number list
outputBuffer is number list
inputCount is number
relBase is number
haltSignal is number
local data:
opcode is text
mode0 is text
mode1 is text
mode2 is text
inA is number
inB is number
out is number
auxNumber is number
procedure:
while 0 is equal to 0 do
store program:pc in opcode
in pc solve pc + 1
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
in pc solve pc + 1
if opcode is equal to "04" then
push inA to outputBuffer
else if opcode is equal to "09" then
in relBase solve inA + relBase
else
call readParameter with mode1 program pc relBase inB
in pc solve pc + 1
if opcode is equal to "01" then
in out solve inA + inB
else if opcode is equal to "02" then
in out solve inA * inB
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
if inA is less than inB then
store 1 in out
else
store 0 in out
end if
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
call saveParameter with mode2 program pc relBase out
in pc solve pc + 1
end if
else # 03
get length of inputBuffer in auxNumber
if inputCount is less than auxNumber then
call saveParameter with mode0 program pc relBase inputBuffer:inputCount
in pc solve pc + 1
in inputCount solve inputCount + 1
else
in pc solve pc - 1
return
end if
end if
repeat
end sub
sub readParameter
parameters:
mode is text
program is number list
pc is number
relBase is number
result is number
procedure:
if mode is equal to "0" then
store program:program:pc in result
else if mode is equal to "1" then
store program:pc in result
else
store program:pc in result
in result solve result + relBase
store program:result in result
end if
end sub
sub saveParameter
parameters:
mode is text
program is number list
pc is number
relBase is number
save is number
local data:
result is number
procedure:
if mode is equal to "0" then
store save in program:program:pc
else
store program:pc in result
in result solve result + relBase
store save in program:result
end if
end sub