-
Notifications
You must be signed in to change notification settings - Fork 1
/
labyrinth.rb
419 lines (369 loc) · 10.6 KB
/
labyrinth.rb
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
# coding: utf-8
require_relative 'point2d'
require_relative 'direction'
class Labyrinth
class ProgramError < Exception; end
OPERATORS = {
' ' => [:wall],
'!' => [:output_int],
'"' => [:nop],
'#' => [:depth],
'$' => [:bit_xor],
'%' => [:mod],
'&' => [:bit_and],
'\'' => [:debug],
'(' => [:dec],
')' => [:inc],
'*' => [:mul],
'+' => [:add],
',' => [:input_char],
'-' => [:sub],
'.' => [:output_char],
'/' => [:div],
'0' => [:digit, 0], '1' => [:digit, 1], '2' => [:digit, 2], '3' => [:digit, 3], '4' => [:digit, 4], '5' => [:digit, 5], '6' => [:digit, 6], '7' => [:digit, 7], '8' => [:digit, 8], '9' => [:digit, 9],
':' => [:dup],
';' => [:pop],
'<' => [:rotate_west],
'=' => [:swap_tops],
'>' => [:rotate_east],
'?' => [:input_int],
'@' => [:terminate],
#'A' => ,
# ...
#'Z' => ,
#'[' => ,
'\\' => [:output_newline],
#']' => ,
'^' => [:rotate_north],
'_' => [:push_zero],
'`' => [:neg],
#'a' => ,
# ...
#'u' => ,
'v' => [:rotate_south],
#'w' => ,
# ...
#'z' => ,
'{' => [:move_to_main],
'|' => [:bit_or],
'}' => [:move_to_aux],
'~' => [:bit_not],
}
OPERATORS.default = [:wall]
def self.run(src, debug_level=0, in_str=$stdin, out_str=$stdout, max_ticks=-1)
new(src, debug_level, in_str, out_str, max_ticks).run
end
def initialize(src, debug_level=false, in_str=$stdin, out_str=$stdout, max_ticks=-1)
@debug_level = debug_level
@in_str = in_str
@out_str = out_str
@max_ticks = max_ticks
@grid = parse(src)
@height = @grid.size
@width = @height == 0 ? 0 : @grid[0].size
@ip = find_start
@dir = East.new
@main = []
@aux = []
@tick = 1
end
def run
if !@ip
return
end
loop do
$stderr.puts "\nTick #{@tick}:" if @debug_level > 1
$stderr.puts @ip.inspect if @debug_level > 1
cmd = cell @ip
$stderr.puts cmd.inspect if @debug_level > 1
if cmd[0] == :terminate
break
end
process cmd
$stderr.puts @main*' ' + ' | ' + @aux.reverse*' ' if @debug_level > 1
@dir = get_new_dir
$stderr.puts @dir.inspect if @debug_level > 1
@ip += @dir.vec
@tick += 1
break if @max_ticks > -1 && @tick > @max_ticks
end
$stderr.puts "\nTicks: #{@tick}" if @debug_level > 0
@max_ticks > -1 && @tick >= @max_ticks
end
private
def parse(src)
lines = src.split($/)
grid = lines.map{|l| l.chars.map{|c| OPERATORS[c]}}
width = grid.map(&:size).max
grid.each{|l| l.fill([:wall], l.length...width)}
end
def find_start
start = nil
@grid.each_with_index do |l,y|
l.each_with_index do |c,x|
if c[0] != :wall
start = Point2D.new(x,y)
break
end
end
if start
break
end
end
start
end
def x
@ip.x
end
def y
@ip.y
end
def cell coords
line = coords.y < 0 ? [] : @grid[coords.y] || []
coords.x < 0 ? [:wall] : line[coords.x] || [:wall]
end
def push_main val
@main << val
end
def push_aux val
@aux << val
end
def pop_main
@main.pop || 0
end
def pop_aux
@aux.pop || 0
end
def peek_main
@main[-1] || 0
end
def process cmd
opcode, param = *cmd
case opcode
# Arithmetic
when :push_zero
push_main 0
when :digit
val = pop_main
if val < 0
push_main(val*10 - param)
else
push_main(val*10 + param)
end
when :inc
push_main(pop_main+1)
when :dec
push_main(pop_main-1)
when :add
push_main(pop_main+pop_main)
when :sub
a = pop_main
b = pop_main
push_main(b-a)
when :mul
push_main(pop_main*pop_main)
when :div
a = pop_main
b = pop_main
push_main(b/a)
when :mod
a = pop_main
b = pop_main
push_main(b%a)
when :neg
push_main(-pop_main)
when :bit_and
push_main(pop_main&pop_main)
when :bit_or
push_main(pop_main|pop_main)
when :bit_xor
push_main(pop_main^pop_main)
when :bit_not
push_main(~pop_main)
# Stack manipulation
when :dup
push_main(peek_main)
when :pop
pop_main
when :move_to_main
push_main(pop_aux)
when :move_to_aux
push_aux(pop_main)
when :swap_tops
a = pop_aux
m = pop_main
push_aux m
push_main a
when :depth
push_main(@main.size)
# I/O
when :input_char
byte = read_byte
push_main(byte ? byte.ord : -1)
when :output_char
@out_str.print (pop_main % 256).chr
when :input_int
val = 0
sign = 1
loop do
byte = read_byte
case byte
when '+'
sign = 1
when '-'
sign = -1
when '0'..'9', nil
@next_byte = byte
else
next
end
break
end
loop do
byte = read_byte
if byte && byte[/\d/]
val = val*10 + byte.to_i
else
@next_byte = byte
break
end
end
push_main(sign*val)
when :output_int
@out_str.print pop_main
when :output_newline
@out_str.puts
# Grid manipulation
when :rotate_west
offset = pop_main
@grid[(y+offset) % @height].rotate!(1)
if offset % @height == 0
@ip += West.new.vec
if x < 0
@ip.x = @width-1
end
end
$stderr.puts @grid.map{|l| l.map{|c| OPERATORS.invert[c]}*''} if @debug_level > 1
when :rotate_east
offset = pop_main
@grid[(y+offset) % @height].rotate!(-1)
if offset % @height == 0
@ip += East.new.vec
if x >= @width
@ip.x = 0
end
end
$stderr.puts @grid.map{|l| l.map{|c| OPERATORS.invert[c]}*''} if @debug_level > 1
when :rotate_north
offset = pop_main
grid = @grid.transpose
grid[(x+offset) % @width].rotate!(1)
@grid = grid.transpose
if offset % @width == 0
@ip += North.new.vec
if y < 0
@ip.y = @height-1
end
end
$stderr.puts @grid.map{|l| l.map{|c| OPERATORS.invert[c]}*''} if @debug_level > 1
when :rotate_south
offset = pop_main
grid = @grid.transpose
grid[(x+offset) % @width].rotate!(-1)
@grid = grid.transpose
if offset % @width == 0
@ip += South.new.vec
if y >= @height
@ip.y = 0
end
end
$stderr.puts @grid.map{|l| l.map{|c| OPERATORS.invert[c]}*''} if @debug_level > 1
# Others
when :terminate
raise '[BUG] Received :terminate. This shouldn\'t happen.'
when :nop
# Nop(e)
when :debug
if @debug_level > 0
$stderr.puts
$stderr.puts "Grid:"
$stderr.puts @grid.map{|l| l.map{|c| OPERATORS.invert[c]}*''}
$stderr.puts "Position: #{@ip.pretty}"
$stderr.puts "Direction: #{@dir.class.name}"
$stderr.puts "Main [ #{@main*' '} | #{@aux.reverse*' '} ] Auxiliary"
end
end
end
def get_new_dir
neighbors = []
[North.new,
East.new,
South.new,
West.new].each do |dir|
neighbors << dir if cell(@ip + dir.vec)[0] != :wall
end
$stderr.puts neighbors.inspect if @debug_level > 1
case neighbors.size
when 0
# Remain where you are by moving back one step.
# This can only happen at the start or due to shifting.
@ip += @dir.reverse.vec
@dir
when 1
# Move in the only possible direction
neighbors[0]
when 2
neighbors = neighbors.select {|d| d.reverse != @dir}
# If we came from one of the two directions, pick the other.
# Otherwise, keep moving straight ahead (this can only happen
# at the start or due to shifting).
if neighbors.size == 2
val = peek_main
if neighbors.include? @dir
@dir
elsif val < 0
@dir.left
elsif val > 0
@dir.right
else
neighbors.sample
end
else
neighbors[0]
end
when 3
val = peek_main
if val < 0
dir = @dir.left
elsif val == 0
dir = @dir
else
dir = @dir.right
end
if !neighbors.include? dir
dir = dir.reverse
end
dir
when 4
val = peek_main
if val < 0
@dir.left
elsif val == 0
@dir
else
@dir.right
end
end
end
def read_byte
result = nil
if @next_byte
result = @next_byte
@next_byte = nil
else
result = @in_str.read(1)
# result = @in_str.read(1) while result =~ /\r|\n/
end
result
end
end