From 793208128854631287ccf2c8f60bf477974c10fb Mon Sep 17 00:00:00 2001 From: Anthony Hayward Date: Wed, 13 May 2020 08:58:54 +0100 Subject: [PATCH] Day 11 Python --- day11_robot/python/README.md | 14 +++++ day11_robot/python/day11_robot_part_1.py | 17 ++---- day11_robot/python/day11_robot_part_2.py | 75 ++++++++++++++++++++++++ 3 files changed, 95 insertions(+), 11 deletions(-) create mode 100644 day11_robot/python/day11_robot_part_2.py diff --git a/day11_robot/python/README.md b/day11_robot/python/README.md index e69de29..bc7951c 100644 --- a/day11_robot/python/README.md +++ b/day11_robot/python/README.md @@ -0,0 +1,14 @@ +``` +python day11_robot_part_1.py +# 1747 + + +python day11_robot_part_2.py + + XXXXXXXX XXXX XXXX XXXXXX XX XX XX XX XX XXXXXX + XX XX XX XX XX XX XX XX XX XX XX XX XX XX + XX XX XX XX XX XXXXXXXX XXXX XX XXXXXX + XX XX XX XXXX XXXXXX XX XX XX XX XX XX XX + XX XX XX XX XX XX XX XX XX XX XX XX XX XX + XXXXXXXX XXXX XXXXXX XX XX XX XX XX XX XXXXXXXX XXXXXX +``` \ No newline at end of file diff --git a/day11_robot/python/day11_robot_part_1.py b/day11_robot/python/day11_robot_part_1.py index 95ad29a..01d67c4 100644 --- a/day11_robot/python/day11_robot_part_1.py +++ b/day11_robot/python/day11_robot_part_1.py @@ -6,21 +6,16 @@ class Robot: def __init__(self, init_mem): - self.computer = IntCodeComputer(init_mem, [0]) + self.computer = IntCodeComputer(init_mem, []) self.pos = (0, 0) self.direction = (0, -1) - self.painted_panels = [] + self.painted_panels = {} def get_panel_colour(self): - for (coords, colour) in self.painted_panels: - if coords == self.pos: - return colour - return 0 # black + return self.painted_panels.get(self.pos, 0) # painted colour or black if never painted def paint_panel(self, colour): - self.painted_panels = \ - [(coords, colour) for (coords, colour) in self.painted_panels if coords != self.pos] + \ - [(self.pos, colour)] + self.painted_panels[self.pos] = colour def run(self): self.computer.inputs = [self.get_panel_colour()] @@ -33,10 +28,10 @@ def run(self): (xd, yd) = self.direction if new_direction == 0: # turn left - self.direction = (-yd, xd) + self.direction = (yd, -xd) elif new_direction == 1: # turn right - self.direction = (yd, -xd) + self.direction = (-yd, xd) else: raise Exception("Unrecognised direction code") (x, y) = self.pos diff --git a/day11_robot/python/day11_robot_part_2.py b/day11_robot/python/day11_robot_part_2.py new file mode 100644 index 0000000..38e74c6 --- /dev/null +++ b/day11_robot/python/day11_robot_part_2.py @@ -0,0 +1,75 @@ +#!/bin/python + +import sys +import io +from computer import IntCodeComputer + +class Robot: + def __init__(self, init_mem): + self.computer = IntCodeComputer(init_mem, []) + self.pos = (0, 0) + self.direction = (0, -1) + self.painted_panels = {} + # initial panel is white + self.paint_panel(1) + + def get_panel_colour(self, position): + return self.painted_panels.get(position, 0) # painted colour or black if never painted + + def paint_panel(self, colour): + self.painted_panels[self.pos] = colour + + def run(self): + self.computer.inputs = [self.get_panel_colour(self.pos)] + self.computer.run() + if self.computer.is_halted(): + return False + else: + [new_colour, new_direction] = self.computer.outputs + self.paint_panel(new_colour) + (xd, yd) = self.direction + if new_direction == 0: + # turn left + self.direction = (yd, -xd) + elif new_direction == 1: + # turn right + self.direction = (-yd, xd) + else: + raise Exception("Unrecognised direction code") + (x, y) = self.pos + (xd, yd) = self.direction + self.pos = (x + xd, y + yd) + return True + + def print_panel_paint_pattern(self): + (x_min, y_min, x_max, y_max) = (0, 0, 0, 0) + for (x, y) in self.painted_panels: + if x < x_min: + x_min = x + elif x > x_max: + x_max = x + if y < y_min: + y_min = y + elif y > y_max: + y_max = y + for y in range(y_min, y_max + 1): + l = "" + for x in range(x_min, x_max + 1): + l += "XX" if self.get_panel_colour((x, y)) else " " + print(l) + + +file_path = "../data/puzzle_input.csv" +init_mem = [] +with io.open(file_path, "r") as f: + line = f.readline() + if line: + init_mem = [int(x.strip()) for x in line.split(",")] + else: + raise Exception("No program in file!") + +robot = Robot(init_mem) +while robot.run(): + pass + +robot.print_panel_paint_pattern()