Day 13 part 2

This commit is contained in:
Adrien Abraham 2019-12-13 14:00:13 +01:00
parent 7f284c70e1
commit de1fe6db13

View file

@ -1,3 +1,5 @@
import time
from intcode import Intcode
@ -6,10 +8,16 @@ def main():
text = input_file.readlines()[0].rstrip()
codes = Intcode.parse_input(text)
game = Game(codes, debug=False)
# Part 1
game = Game(codes)
game.run()
print("Num block tiles:", sum(sum(v == Game.TILE_BLOCK for v in row) for row in game.tiles))
# Part 2 (autoplay)
codes[0] = 2
game = DemoGame(codes)
game.run()
class Game(Intcode):
@ -21,29 +29,75 @@ class Game(Intcode):
TILE_HPAD = 3
TILE_BALL = 4
TILES_CHARS = {
TILE_EMPTY: " ",
TILE_WALL: "",
TILE_BLOCK: "",
TILE_HPAD: "",
TILE_BALL: "o",
}
OUT_X = 0
OUT_Y = 1
OUT_T = 2
JOY_L = -1
JOY_N = 0
JOY_R = 1
def __init__(self, codes, debug=False):
super().__init__(codes, debug=debug)
self.tiles = [[Game.TILE_EMPTY] * Game.TILES_DIM for _ in range(Game.TILES_DIM)]
self.output_type = Game.OUT_X
self.output_pos = [0, 0]
def input_data(self):
self.print_tiles()
return int({
"h": Game.JOY_L,
"l": Game.JOY_R,
}.get(input(), Game.JOY_N))
def output_data(self, data):
if self.output_type in [self.OUT_X, self.OUT_Y]:
self.output_pos[self.output_type] = data
elif self.output_type == self.OUT_T:
if self.output_pos == [-1, 0]:
print("Score:", data)
else:
self.tiles[self.output_pos[1]][self.output_pos[0]] = data
self.output_type = (self.output_type + 1) % 3
def print_tiles(self):
for row in self.tiles:
if row[0] == Game.TILE_EMPTY:
break
for value in row:
print(str(value), end="")
print(Game.TILES_CHARS[value], end="")
print()
class DemoGame(Game):
def input_data(self):
ball_pos = self.get_unique_tile_pos(Game.TILE_BALL)
pad_pos = self.get_unique_tile_pos(Game.TILE_HPAD)
self.print_tiles()
time.sleep(0.1)
if ball_pos[1] > pad_pos[1]:
return Game.JOY_R
elif ball_pos[1] < pad_pos[1]:
return Game.JOY_L
else:
return Game.JOY_N
def get_unique_tile_pos(self, tile_id):
for x, row in enumerate(self.tiles):
for y, value in enumerate(row):
if value == tile_id:
return [x, y]
raise ValueError("Could not find {}".format(tile_id))
if __name__ == "__main__":
main()