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
|
import intcode
import time
_input = open("../input/11","r").readlines()
program = [int(x) for x in _input[0].split(",")]
x, y = 0, 0
direction = 0
# 0 is up
# 1 is left
# 2 is down
# 3 is right
# 4 is up (again)
# turning left is direction += 1
# turning right is direction -= 1
# direction is direction % 4
painted = 0
colors = {(0,0): 1} # (x,y): 1/0 (1 = white, 0 = black)
got_color = False
def draw(colors, ship, direction):
min_x, max_x, min_y, max_y = 0, 0, 0, 0
ship_c = ""
if direction == 0:
ship_c = "^"
elif direction == 1:
ship_c = "<"
elif direction == 2:
ship_c = "v"
elif direction == 3:
ship_c = ">"
for color in colors:
min_x = min(min_x, color[0])
max_x = max(max_x, color[0])
min_y = min(min_y, color[1])
max_y = max(max_y, color[1])
for y in range(min_y-5, max_y+5):
for x in range(min_x-5, max_x+5):
if (x,y) == ship:
print(ship_c, end="")
elif (x,y) in colors:
c = colors[(x,y)]
print("\u2588" if c == 1 else ".", end="")
else:
print(" ", end="")
print("")
c = intcode.Computer(program)
while c.memory[c.pointer] != 99:
# input
#print(x, y)
#draw(colors, (x,y), direction % 4)
#input()
#time.sleep(0.1)
c.input = colors.get((x, y), 0)
c.step()
if c.output is not None:
if not got_color:
colors[(x, y)] = c.output
c.output = None
got_color = True
elif got_color:
direction += (1 if c.output == 0 else -1)
dir = direction % 4
if dir == 0:
y -= 1
elif dir == 1:
x -= 1
elif dir == 2:
y += 1
elif dir == 3:
x += 1
got_color = False
c.output = None
draw(colors, (0,0), 0)
print(colors)
print(len(colors))
|