From 7f284c70e1faaa5c70f264c1cc53705714ebe0c3 Mon Sep 17 00:00:00 2001 From: Adrien Abraham Date: Fri, 13 Dec 2019 13:30:34 +0100 Subject: [PATCH] Day 13 --- 2019/day13.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 2019/day13.py diff --git a/2019/day13.py b/2019/day13.py new file mode 100644 index 0000000..18ef7cb --- /dev/null +++ b/2019/day13.py @@ -0,0 +1,49 @@ +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) + + game = Game(codes, debug=False) + game.run() + print("Num block tiles:", sum(sum(v == Game.TILE_BLOCK for v in row) for row in game.tiles)) + + +class Game(Intcode): + + TILES_DIM = 64 + + TILE_EMPTY = 0 + TILE_WALL = 1 + TILE_BLOCK = 2 + TILE_HPAD = 3 + TILE_BALL = 4 + + OUT_X = 0 + OUT_Y = 1 + OUT_T = 2 + + 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 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: + 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: + for value in row: + print(str(value), end="") + print() + + +if __name__ == "__main__": + main()