-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path201913.py
55 lines (47 loc) · 1.31 KB
/
201913.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
from collections import defaultdict
from enum import IntEnum
from intcode import InputInterrupt, OutputInterrupt, intcode_from_file
class Tile(IntEnum):
EMPTY = 0
WALL = 1
BLOCK = 2
PADDLE = 3
BALL = 4
screen = defaultdict(lambda: Tile.EMPTY)
game = intcode_from_file("201913.txt")
out = []
while not game.halted:
try:
game.run()
except OutputInterrupt:
out.append(game.output.popleft())
match out:
case [x, y, tile]:
screen[(x, y)] = Tile(tile)
out.clear()
print(f"Part one: {sum(1 for tile in screen.values() if tile == Tile.BLOCK)}")
game = intcode_from_file("201913.txt", mod={0: 2})
out = []
score = 0
ball_x = 0
paddle_x = 0
while not game.halted:
try:
game.run()
except OutputInterrupt:
out.append(game.pop_output())
match out:
case [-1, 0, tile]:
score = tile
out.clear()
case [x, _, Tile.PADDLE]:
paddle_x = x
out.clear()
case [x, _, Tile.BALL]:
ball_x = x
out.clear()
case [_, _, _]:
out.clear()
except InputInterrupt:
game.append_input(-1 if ball_x < paddle_x else 1 if ball_x > paddle_x else 0)
print(f"Part two: {score}")