2019-12-13 14:00:13 +01:00
|
|
|
import time
|
|
|
|
|
2019-12-13 13:30:34 +01:00
|
|
|
from intcode import Intcode
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
with open("day13.txt", "rt") as input_file:
|
|
|
|
text = input_file.readlines()[0].rstrip()
|
|
|
|
codes = Intcode.parse_input(text)
|
|
|
|
|
2019-12-13 14:00:13 +01:00
|
|
|
# Part 1
|
|
|
|
game = Game(codes)
|
2019-12-13 13:30:34 +01:00
|
|
|
game.run()
|
|
|
|
print("Num block tiles:", sum(sum(v == Game.TILE_BLOCK for v in row) for row in game.tiles))
|
|
|
|
|
2019-12-13 14:00:13 +01:00
|
|
|
# Part 2 (autoplay)
|
|
|
|
codes[0] = 2
|
|
|
|
game = DemoGame(codes)
|
|
|
|
game.run()
|
|
|
|
|
2019-12-13 13:30:34 +01:00
|
|
|
|
|
|
|
class Game(Intcode):
|
|
|
|
|
|
|
|
TILES_DIM = 64
|
|
|
|
|
|
|
|
TILE_EMPTY = 0
|
|
|
|
TILE_WALL = 1
|
|
|
|
TILE_BLOCK = 2
|
|
|
|
TILE_HPAD = 3
|
|
|
|
TILE_BALL = 4
|
|
|
|
|
2019-12-13 14:00:13 +01:00
|
|
|
TILES_CHARS = {
|
|
|
|
TILE_EMPTY: " ",
|
|
|
|
TILE_WALL: "█",
|
|
|
|
TILE_BLOCK: "░",
|
|
|
|
TILE_HPAD: "▔",
|
|
|
|
TILE_BALL: "o",
|
|
|
|
}
|
|
|
|
|
2019-12-13 13:30:34 +01:00
|
|
|
OUT_X = 0
|
|
|
|
OUT_Y = 1
|
|
|
|
OUT_T = 2
|
|
|
|
|
2019-12-13 14:00:13 +01:00
|
|
|
JOY_L = -1
|
|
|
|
JOY_N = 0
|
|
|
|
JOY_R = 1
|
|
|
|
|
2019-12-13 13:30:34 +01:00
|
|
|
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]
|
|
|
|
|
2019-12-13 14:00:13 +01:00
|
|
|
def input_data(self):
|
|
|
|
self.print_tiles()
|
|
|
|
return int({
|
|
|
|
"h": Game.JOY_L,
|
|
|
|
"l": Game.JOY_R,
|
|
|
|
}.get(input(), Game.JOY_N))
|
|
|
|
|
2019-12-13 13:30:34 +01:00
|
|
|
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:
|
2019-12-13 14:00:13 +01:00
|
|
|
if self.output_pos == [-1, 0]:
|
|
|
|
print("Score:", data)
|
|
|
|
else:
|
|
|
|
self.tiles[self.output_pos[1]][self.output_pos[0]] = data
|
2019-12-13 13:30:34 +01:00
|
|
|
self.output_type = (self.output_type + 1) % 3
|
|
|
|
|
|
|
|
def print_tiles(self):
|
|
|
|
for row in self.tiles:
|
2019-12-13 14:00:13 +01:00
|
|
|
if row[0] == Game.TILE_EMPTY:
|
|
|
|
break
|
2019-12-13 13:30:34 +01:00
|
|
|
for value in row:
|
2019-12-13 14:00:13 +01:00
|
|
|
print(Game.TILES_CHARS[value], end="")
|
2019-12-13 13:30:34 +01:00
|
|
|
print()
|
|
|
|
|
|
|
|
|
2019-12-13 14:00:13 +01:00
|
|
|
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))
|
|
|
|
|
|
|
|
|
2019-12-13 13:30:34 +01:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|