Skip to content

Commit

Permalink
Day 11 Python
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthony Hayward committed May 13, 2020
1 parent 8a7c726 commit 7932081
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 11 deletions.
14 changes: 14 additions & 0 deletions day11_robot/python/README.md
Original file line number Diff line number Diff line change
@@ -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
```
17 changes: 6 additions & 11 deletions day11_robot/python/day11_robot_part_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()]
Expand All @@ -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
Expand Down
75 changes: 75 additions & 0 deletions day11_robot/python/day11_robot_part_2.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 7932081

Please sign in to comment.