-
Notifications
You must be signed in to change notification settings - Fork 0
/
13.py
129 lines (100 loc) · 2.66 KB
/
13.py
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
from intcode import *
import keyboard
from time import sleep
input_code = open('13.input', 'r').readline().strip()
# Part 1
stream = Streams(["A","B"])
intcode = IntCode(input_code)
program = Program(intcode, name="A", stream=stream)
output = []
for out in program.run():
output.append(out)
print("Part 1: %d" % len([x for i,x in enumerate(output) if (i+1) % 3 == 0 and x == 2]))
# Part 2
tile_types = {
0: " ",
1: "#",
2: "o",
3: "-",
4: "@",
}
def render(output,points, bot_playing):
max_x = 43
max_y = 19
for i in range(0,len(output),3):
points[(output[i], output[i+1])] = output[i+2]
#max_x = max_x if max_x > output[i] else output[i]
#max_y = max_y if max_y > output[i+1] else output[i+1]
tiles_left = 0
ball = None
paddle = None
for y in range(max_y+1):
row = ""
for x in range(max_x+1):
if (x,y) in points:
tile = points[(x,y)]
row += tile_types[tile]
if tile == 3:
paddle = (x,y)
if tile == 4:
ball = (x,y)
if points[(x,y)] == 2:
tiles_left += 1
else:
row += " "
if not bot_playing:
print row
score = None
if (-1,0) in points:
score = points[(-1,0)]
if not bot_playing:
print "Score: %d" % score
return tiles_left, ball, paddle, score
stream = Streams(["A"])
intcode = IntCode("2" + input_code[1:])
program = Program(intcode, name="A", stream=stream)
import sys,tty
tiles_left = 99
points = {}
fd = sys.stdin.fileno()
old = tty.tcgetattr(fd)
import atexit
def exit_handler():
tty.tcsetattr(fd, tty.TCSAFLUSH, old)
atexit.register(exit_handler)
game_started = False
bot_playing = True
last_score = 0
while tiles_left > 0:
output = []
for out in program.run():
output.append(out)
tiles_left, ball, paddle, score = render(output, points, bot_playing)
last_score = score
if score != 0:
game_started = True
if game_started and score == 0:
print "GAME OVER"
exit()
move = 0
m = " "
if bot_playing:
if paddle[0] == ball[0]:
move = 0
elif paddle[0] > ball[0]:
move = -1
else:
move = 1
else:
tty.setcbreak(sys.stdin)
key = ord(sys.stdin.read(1))
if key==97: # a
tty.tcsetattr(fd, tty.TCSAFLUSH, old)
exit()
if key==104: # h
move = -1
if key==116: # t
move = 1
stream.add("A", move)
#sleep(0.1)
print("Part 2: %d" % last_score)