Skip to content

Commit

Permalink
Added solution for Day 11
Browse files Browse the repository at this point in the history
  • Loading branch information
LukeMoll committed Dec 11, 2019
1 parent 5a2a333 commit 3b8568a
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
82 changes: 82 additions & 0 deletions day11.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import aoc
from intcode import IntcodeMachine
from instruction import *
from day08 import blocks4, print_image4, Size

from typing import List, Tuple, Set
from operator import itemgetter

Coord = Tuple[int]

INSTRUCTIONS = {
i.OPCODE : i for i in [IAdd, IMult, IHalt, IInput, IOutput, IJumpNZ, IJumpZ, ILessThan, IEquals, IAdjustOffset]
}

def main():
aoc.header("Space Police")

program = list(map(int, aoc.get_input().readline().split(",")))
aoc.output(1, run_robot, args=[program, False], post=lambda t:len(t[1]))
aoc.output(2, run_robot, args=[program, True], output=part2_post)


directions_clockwise = [
(0,-1),
(1,0),
(0,1),
(-1,0)
]

def run_robot(prog : List[int], start_on_white):
white_panels = {(0,0)} if start_on_white else set()
painted_panels = set()

location = (0,0)
direction = directions_clockwise[0]
m = IntcodeMachine(prog, INSTRUCTIONS)

while m.running:
try:
m.input.append(1 if location in white_panels else 0)
while m.step(): pass
except EOFError as e:
paint = m.output.pop(0)
if paint == 0:
white_panels -= {location}
else:
white_panels.add(location)

painted_panels.add(location)

turn = m.output.pop(0)
x = 1 if turn == 1 else -1
direction = directions_clockwise[(directions_clockwise.index(direction)+x)%4]

location = (location[0] + direction[0], location[1] + direction[1])

return (white_panels, painted_panels)

def coords_to_img(coords : Set[Coord]):
X = set(map(itemgetter(0), coords))
Y = set(map(itemgetter(1), coords))
corner_tl = (
min(X), min(Y)
)
corner_br = (
max(X), max(Y)
)
coords = set(map(lambda c: (c[0]-corner_tl[0], c[1]-corner_tl[1]), coords))
s = Size(corner_br[0]-corner_tl[0]+1,corner_br[1]-corner_tl[1]+1)
img = [
0 for _ in range(s.width * s.height)
]
for x,y in coords:
img[x + y * s.width] = 1
return img, s

def part2_post(t):
(white_panels, printed_panels) = t
print_image4(*coords_to_img(white_panels))

if __name__ == "__main__":
main()
2 changes: 1 addition & 1 deletion intcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def fetch(self, op, mode : AddressingMode):
elif op < 0:
raise RuntimeError(f"Negative access at address {op}")
else:
print(f"Uninitialised access at address {op}, returning 0")
# print(f"Uninitialised access at address {op}, returning 0")
return 0

def store(self, address : int, value : int, mode : AddressingMode):
Expand Down

0 comments on commit 3b8568a

Please sign in to comment.