From 7afab1ad3892084ee079fe5ef45edc7fb452af06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Wed, 11 Dec 2019 06:51:34 +0100 Subject: Day 11 py I found a bug in my intcode-implementation which day 9 didn't catch. To solve it, I switched the memory from my past hybrid solution (initial memory as a list and extra memory as a dict) to a 100% dict-solution. I am not entirely sure why it didn't work before since Computer.write() checks if the write-position is outside the length, unless it got a negative address. It seems like dict is slower than list for memory so I might try to re-implement lists for (initial) memory later. --- solutions/input/11 | 2 ++ solutions/py/d11.py | 79 +++++++++++++++++++++++++++++++++++++++++++++++++ solutions/py/intcode.py | 15 ++++------ 3 files changed, 86 insertions(+), 10 deletions(-) create mode 100644 solutions/input/11 create mode 100644 solutions/py/d11.py diff --git a/solutions/input/11 b/solutions/input/11 new file mode 100644 index 0000000..6ecf042 --- /dev/null +++ b/solutions/input/11 @@ -0,0 +1,2 @@ +3,8,1005,8,329,1106,0,11,0,0,0,104,1,104,0,3,8,102,-1,8,10,1001,10,1,10,4,10,1008,8,0,10,4,10,1002,8,1,29,2,1102,1,10,1,1009,16,10,2,4,4,10,1,9,5,10,3,8,1002,8,-1,10,101,1,10,10,4,10,108,0,8,10,4,10,101,0,8,66,2,106,7,10,1006,0,49,3,8,1002,8,-1,10,101,1,10,10,4,10,108,1,8,10,4,10,1002,8,1,95,1006,0,93,3,8,102,-1,8,10,1001,10,1,10,4,10,108,1,8,10,4,10,102,1,8,120,1006,0,61,2,1108,19,10,2,1003,2,10,1006,0,99,3,8,1002,8,-1,10,1001,10,1,10,4,10,1008,8,0,10,4,10,101,0,8,157,3,8,102,-1,8,10,1001,10,1,10,4,10,1008,8,1,10,4,10,1001,8,0,179,2,1108,11,10,1,1102,19,10,3,8,102,-1,8,10,1001,10,1,10,4,10,1008,8,1,10,4,10,101,0,8,209,2,108,20,10,3,8,1002,8,-1,10,101,1,10,10,4,10,108,1,8,10,4,10,101,0,8,234,3,8,102,-1,8,10,101,1,10,10,4,10,108,0,8,10,4,10,1002,8,1,256,2,1102,1,10,1006,0,69,2,108,6,10,2,4,13,10,3,8,102,-1,8,10,101,1,10,10,4,10,1008,8,0,10,4,10,1002,8,1,294,1,1107,9,10,1006,0,87,2,1006,8,10,2,1001,16,10,101,1,9,9,1007,9,997,10,1005,10,15,99,109,651,104,0,104,1,21101,387395195796,0,1,21101,346,0,0,1105,1,450,21101,0,48210129704,1,21101,0,357,0,1105,1,450,3,10,104,0,104,1,3,10,104,0,104,0,3,10,104,0,104,1,3,10,104,0,104,1,3,10,104,0,104,0,3,10,104,0,104,1,21101,0,46413147328,1,21102,404,1,0,1106,0,450,21102,179355823323,1,1,21101,415,0,0,1105,1,450,3,10,104,0,104,0,3,10,104,0,104,0,21102,1,838345843476,1,21101,0,438,0,1105,1,450,21101,709475709716,0,1,21101,449,0,0,1105,1,450,99,109,2,22102,1,-1,1,21102,40,1,2,21101,0,481,3,21101,0,471,0,1105,1,514,109,-2,2105,1,0,0,1,0,0,1,109,2,3,10,204,-1,1001,476,477,492,4,0,1001,476,1,476,108,4,476,10,1006,10,508,1101,0,0,476,109,-2,2106,0,0,0,109,4,2101,0,-1,513,1207,-3,0,10,1006,10,531,21101,0,0,-3,21201,-3,0,1,21201,-2,0,2,21101,1,0,3,21101,550,0,0,1105,1,555,109,-4,2106,0,0,109,5,1207,-3,1,10,1006,10,578,2207,-4,-2,10,1006,10,578,21201,-4,0,-4,1105,1,646,22101,0,-4,1,21201,-3,-1,2,21202,-2,2,3,21101,597,0,0,1105,1,555,22102,1,1,-4,21101,0,1,-1,2207,-4,-2,10,1006,10,616,21101,0,0,-1,22202,-2,-1,-2,2107,0,-3,10,1006,10,638,22102,1,-1,1,21101,638,0,0,106,0,513,21202,-2,-1,-2,22201,-4,-2,-4,109,-5,2106,0,0 + diff --git a/solutions/py/d11.py b/solutions/py/d11.py new file mode 100644 index 0000000..88d942f --- /dev/null +++ b/solutions/py/d11.py @@ -0,0 +1,79 @@ +import intcode +import time + +_input = open("../input/11","r").readlines() + +program = [int(x) for x in _input[0].split(",")] +x, y = 0, 0 +direction = 0 +# 0 is up +# 1 is left +# 2 is down +# 3 is right +# 4 is up (again) +# turning left is direction += 1 +# turning right is direction -= 1 +# direction is direction % 4 +painted = 0 +colors = {(0,0): 1} # (x,y): 1/0 (1 = white, 0 = black) + +got_color = False + +def draw(colors, ship, direction): + min_x, max_x, min_y, max_y = 0, 0, 0, 0 + ship_c = "" + if direction == 0: + ship_c = "^" + elif direction == 1: + ship_c = "<" + elif direction == 2: + ship_c = "v" + elif direction == 3: + ship_c = ">" + for color in colors: + min_x = min(min_x, color[0]) + max_x = max(max_x, color[0]) + min_y = min(min_y, color[1]) + max_y = max(max_y, color[1]) + for y in range(min_y-5, max_y+5): + for x in range(min_x-5, max_x+5): + if (x,y) == ship: + print(ship_c, end="") + elif (x,y) in colors: + c = colors[(x,y)] + print("\u2588" if c == 1 else ".", end="") + else: + print(" ", end="") + print("") + + +c = intcode.Computer(program) +while c.memory[c.pointer] != 99: + # input + #print(x, y) + #draw(colors, (x,y), direction % 4) + #input() + #time.sleep(0.1) + c.input = colors.get((x, y), 0) + c.step() + if c.output is not None: + if not got_color: + colors[(x, y)] = c.output + c.output = None + got_color = True + elif got_color: + direction += (1 if c.output == 0 else -1) + dir = direction % 4 + if dir == 0: + y -= 1 + elif dir == 1: + x -= 1 + elif dir == 2: + y += 1 + elif dir == 3: + x += 1 + got_color = False + c.output = None +draw(colors, (0,0), 0) +print(colors) +print(len(colors)) diff --git a/solutions/py/intcode.py b/solutions/py/intcode.py index 0f17962..aee72ee 100644 --- a/solutions/py/intcode.py +++ b/solutions/py/intcode.py @@ -13,8 +13,9 @@ HAL = 99 class Computer(object): def __init__(self, program): - self.memory = program.copy() - self.memory_size = len(self.memory) # pointers greater than this go in extra memory + self.memory = {} + for i in range(len(program)): + self.memory[i] = program[i] self.pointer = 0 self.phase_read = False # for day 7 self.relative_base = 0 @@ -35,16 +36,10 @@ class Computer(object): self.output = None def write(self, addr, val): - if addr > self.memory_size: - self.extra_mem[addr] = val - else: - self.memory[addr] = val + self.memory[addr] = val def get(self, addr): - if addr > self.memory_size: - return self.extra_mem[addr] if addr in self.extra_mem else 0 - else: - return self.memory[addr] + return self.memory.get(addr, 0) def get_param(self, inst, num): if inst[num] == 0: -- cgit v1.2.1