diff options
| author | Gustav Sörnäs <gusso230@student.liu.se> | 2019-12-11 07:55:11 +0100 |
|---|---|---|
| committer | Gustav Sörnäs <gusso230@student.liu.se> | 2019-12-11 07:55:18 +0100 |
| commit | 5366f49af365059e0d791290058aaae606f642ed (patch) | |
| tree | e9ce9e606282d43f27415d9b1e5540b4bfef56c7 /solutions/py/d11.py | |
| parent | bef82d77ceee6f34b9e50baf61461e7f3d5a6d6d (diff) | |
| download | aoc-5366f49af365059e0d791290058aaae606f642ed.tar.gz | |
Initial animation
Run with 'python d11.py'
Diffstat (limited to 'solutions/py/d11.py')
| -rw-r--r-- | solutions/py/d11.py | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/solutions/py/d11.py b/solutions/py/d11.py index c97f3a4..d0b6094 100644 --- a/solutions/py/d11.py +++ b/solutions/py/d11.py @@ -114,6 +114,46 @@ def pt2(input): c.output = None return draw(colors, (0,0), 0) +def visualize(input): + program = [int(x) for x in input[0].split(",")] + x=y = 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 + colors = {(0,0): 1} # (x,y): 1/0 (1 = white, 0 = black) + + got_color = False + c = intcode.Computer(program) + while c.memory[c.pointer] != 99: + time.sleep(0.002) + print(draw(colors, (x,y), direction % 4)) + 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 + if __name__ == "__main__": import cProfile @@ -122,3 +162,4 @@ if __name__ == "__main__": cProfile.run("pt2(input)") print(pt1(input)) print(pt2(input)) + visualize(input) |
