Skip to content

Commit 0cf706e

Browse files
committed
Day 13 part 2
1 parent c5166f1 commit 0cf706e

File tree

1 file changed

+70
-20
lines changed

1 file changed

+70
-20
lines changed

day13/d13.py

+70-20
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,87 @@
1-
import sys
1+
import sys, time
22

33
sys.path.append('../common')
44
import intcode
55

6+
# Delay time in ms.
7+
ANIMATION_SPEED = 1
8+
69
TILE_EMPTY = 0
710
TILE_WALL = 1
811
TILE_BLOCK = 2
912
TILE_PADDLE = 3
1013
TILE_BALL = 4
1114

15+
W = 40
16+
H = 20
17+
18+
ball = 0
19+
paddle = 0
20+
21+
def save_tile(x,y,tile):
22+
screen[(x,y)] = tile
23+
24+
def print_tile(x,y):
25+
global screen, ball, paddle
26+
if (x,y)==(-1,0):
27+
score = screen[(-1, 0)] if (-1,0) in screen else 0
28+
c = f'Score: {score:05} Blocks left: {count_blocks():03}'
29+
elif screen[(x,y)] == TILE_EMPTY:
30+
c = ' '
31+
elif screen[(x,y)] == TILE_WALL:
32+
c = '⬛️'
33+
elif screen[(x,y)] == TILE_BLOCK:
34+
c = '⬜️'
35+
elif screen[(x,y)] == TILE_PADDLE:
36+
c = '=='
37+
paddle = x
38+
elif screen[(x,y)] == TILE_BALL:
39+
c = '⚫'
40+
ball = x
41+
if x==-1:
42+
pos = '\33[25;0H'
43+
else:
44+
pos = '\33[' + str(y+4) + ';' + str(x*2+1) + 'H'
45+
sys.stdout.write(pos + c + '\33[25;0H')
46+
sys.stdout.flush()
47+
48+
def print_screen():
49+
print_tile(-1,0)
50+
for y in range(H):
51+
for x in range(W):
52+
print_tile(x,y)
53+
54+
def print_more(input):
55+
output = intcode.runIntCode(input)
56+
for i in range(len(output)//3):
57+
save_tile(*output[i*3:i*3+3])
58+
print_tile(*output[i*3:i*3+2])
59+
if output[i*3+2]:
60+
# No sleep if it is an erase instruction
61+
time.sleep(ANIMATION_SPEED/1000)
62+
63+
def count_blocks():
64+
return sum([tile==TILE_BLOCK for tile in screen.values()])
65+
1266
values = list(map(int, open('d13.in').read().strip().split(',')))
1367
intcode.init(values)
1468
output = intcode.runIntCode([])
1569
screen = {}
1670
for i in range(len(output)//3):
17-
x,y, tile = output[i*3:i*3+3]
18-
screen[(x,y)] = tile
19-
20-
def print_screen():
21-
for y in range(20):
22-
for x in range(40):
23-
if screen[(x,y)] == TILE_EMPTY:
24-
c = ' '
25-
elif screen[(x,y)] == TILE_WALL:
26-
c = '⬛️'
27-
elif screen[(x,y)] == TILE_BLOCK:
28-
c = '⬜️'
29-
elif screen[(x,y)] == TILE_PADDLE:
30-
c = '=='
31-
elif screen[(x,y)] == TILE_BALL:
32-
c = '⚫'
33-
print(c, end='')
34-
print()
71+
save_tile(*output[i*3:i*3+3])
3572

73+
sys.stdout.write('\33[2J')
74+
print('\33[0;0HPart 1:', count_blocks())
3675
print_screen()
37-
print('Part 1:', sum([tile==TILE_BLOCK for tile in screen.values()]))
76+
77+
78+
values[0] = 2
79+
intcode.init(values)
80+
intcode.runIntCode([])
81+
while True:
82+
input = 1 if ball > paddle else -1 if ball < paddle else 0
83+
print_more([input])
84+
if count_blocks()== 0:
85+
break
86+
87+
print('\33[26;0HPart 2:', screen[(-1, 0)])

0 commit comments

Comments
 (0)