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
|
#!/usr/bin/env ruby
class Machine
def initialize(starting_memory = [])
@mem = starting_memory
@pc = 0
@halt = false
@buffered_input = []
@buffered_output = []
@relative_base = 0
end
def mem(start = 0, len = @mem.length)
@mem[start...(start + len)]
end
def halt!
@halt = true
end
def halt?
@halt
end
def buffer_input(x)
if x.is_a?(Array) then
@buffered_input += x
else
@buffered_input.push(x)
end
end
def buffered_output(flush = false)
t = @buffered_output
@buffered_output = [] if flush
t
end
def step
return if halt?
opcode = @mem[@pc] % 100
if opcode == 99 then
halt!
@pc += 1
return
end
p3, p2, p1 = ("%03d" % (@mem[@pc] / 100)).chars
l1w, l1r = get(p1, 1)
l2w, l2r = get(p2, 2) if [1,2,5,6,7,8].include?(opcode)
l3w, l3r = get(p3, 3) if [1,2,7,8].include?(opcode)
case opcode
when 1
@mem[l3w] = l1r + l2r
@pc += 4
when 2
@mem[l3w] = l1r * l2r
@pc += 4
when 3
@mem[l1w] = @buffered_input.shift
@pc += 2
when 4
@buffered_output.push(l1r)
@pc += 2
when 5
@pc = (l1r != 0 ? l2r : @pc + 3)
when 6
@pc = (l1r == 0 ? l2r : @pc + 3)
when 7
@mem[l3w] = (l1r < l2r ? 1 : 0)
@pc += 4
when 8
@mem[l3w] = (l1r == l2r ? 1 : 0)
@pc += 4
when 9
@relative_base += l1r
@pc += 2
end
end
def run
until halt? do
step
end
end
private
def get(p, a)
w = case p
when "0"
@mem[@pc + a]
when "2"
@relative_base + @mem[@pc + a]
end
r = case p
when "0"
@mem[@mem[@pc + a]]
when "1"
@mem[@pc + a]
when "2"
@mem[@relative_base + @mem[@pc + a]]
end
[w, r || 0]
end
end
def turn(d, t)
if t.zero? then
case d
when [0, -1]
[-1, 0]
when [-1, 0]
[0, 1]
when [0, 1]
[1, 0]
when [1, 0]
[0, -1]
end
else
case d
when [0, -1]
[1, 0]
when [-1, 0]
[0, -1]
when [0, 1]
[-1, 0]
when [1, 0]
[0, 1]
end
end
end
def move(l, d)
[l[0] + d[0], l[1] + d[1]]
end
def make2darray(hash)
dx = -hash.keys.map(&:first).min
dy = -hash.keys.map(&:last).min
maxx = hash.keys.map(&:first).max + dx
maxy = hash.keys.map(&:last).max + dy
array = Array.new(maxy + 1) { Array.new(maxx + 1, 0) }
hash.each do |l, p|
array[l.last + dy][l.first + dx] = p
end
array
end
input = $stdin.readlines[0].strip.split(",").map(&:to_i)
m = Machine.new(input)
grid = Hash.new{0}
grid[[0,0]] = 1
l = [0, 0]
d = [0, -1]
loop do
m.buffer_input(grid[l])
until m.halt? || m.buffered_output.length == 2 do
m.step
end
if m.halt? then
break
else
p, t = m.buffered_output(true)
grid[l] = p
d = turn(d, t)
l = move(l, d)
end
end
grid = make2darray(grid)
grid.each_slice(2) do |u, l|
w = u.length
l = Array.new(w, 0) if l.nil?
w.times do |i|
case [u[i], l[i]]
when [0,0]
print " "
when [0,1]
print "▄"
when [1,0]
print "▀"
when [1,1]
print "█"
end
end
puts
end
|