From 729dee98cbdf0593b723dc7eaa6e3adaed724ade Mon Sep 17 00:00:00 2001 From: Dece Date: Tue, 17 Dec 2019 18:16:26 +0100 Subject: [PATCH] Day 17 part 1 --- 2019/day17.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2019/grid.py | 12 ++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 2019/day17.py create mode 100644 2019/grid.py diff --git a/2019/day17.py b/2019/day17.py new file mode 100644 index 0000000..0c22a12 --- /dev/null +++ b/2019/day17.py @@ -0,0 +1,50 @@ +from collections import defaultdict + +from grid import Grid +from intcode import Intcode + + +def main(): + with open("day17.txt", "rt") as input_file: + codes = Intcode.parse_input(input_file.read().rstrip()) + + camera = Camera(codes) + camera.run() + intersections = camera.intersect() + print("Sum:", sum(x * y for x, y in intersections)) + + +class Camera(Intcode): + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.out_x = 0 + self.out_y = 0 + self.img = Grid() + + def output_data(self, data): + if data == ord("\n"): + self.out_x, self.out_y = 0, self.out_y + 1 + else: + self.img.g[self.out_y][self.out_x] = data + self.out_x += 1 + + def intersect(self): + intersections = [] + g = self.img.g + for x, col in list(g.items()).copy(): + for y, v in list(col.items()).copy(): + if all( + e == ord("#") + for e in [v, g[x][y-1], g[x][y+1], g[x+1][y], g[x-1][y]] + ): + print("O", end="") + intersections.append((x, y)) + else: + print(chr(v), end="") + print() + return intersections + + +if __name__ == "__main__": + main() diff --git a/2019/grid.py b/2019/grid.py new file mode 100644 index 0000000..e41e8ec --- /dev/null +++ b/2019/grid.py @@ -0,0 +1,12 @@ +from collections import defaultdict + + +class Grid: + + def __init__(self): + self.g = defaultdict(lambda: defaultdict(int)) + + def values(self): + for a, chunk in self.g.items(): + for b, v in chunk.items(): + yield a, b, v