summaryrefslogtreecommitdiffstats
path: root/solutions/py
diff options
context:
space:
mode:
Diffstat (limited to 'solutions/py')
-rw-r--r--solutions/py/d13.py135
-rw-r--r--solutions/py/intcode.py5
-rw-r--r--solutions/py/main.py3
3 files changed, 142 insertions, 1 deletions
diff --git a/solutions/py/d13.py b/solutions/py/d13.py
new file mode 100644
index 0000000..78fc423
--- /dev/null
+++ b/solutions/py/d13.py
@@ -0,0 +1,135 @@
+import intcode
+import queue
+import time
+
+def pt1(input):
+ c = intcode.Computer([int(x) for x in input[0].split(",")])
+ buffer_out = []
+ screen = {}
+ while c.memory[c.pointer] != 99:
+ c.step()
+ if c.output is not None:
+ buffer_out.append(c.output)
+ c.output = None
+ if len(buffer_out) == 3:
+ screen[(buffer_out[0], buffer_out[1])] = buffer_out[2]
+ buffer_out = []
+ blocks = 0
+ for p in screen:
+ if screen[p] == 2:
+ blocks += 1
+ return blocks
+
+
+def pt2(input):
+ def draw(screen, points):
+ ball_x = 0
+ paddle_x = 0
+ min_x = max_x = min_y = max_y = 0
+ for p in screen:
+ if p == (-1,0):
+ points = screen[p]
+ elif screen[p] == 3:
+ paddle_x = p[0]
+ elif screen[p] == 4:
+ ball_x = p[0]
+ return points, ball_x, paddle_x
+
+ c = intcode.Computer([int(x) for x in input[0].split(",")])
+ c.memory[0] = 2
+
+ screen = {}
+ buffer_out = []
+ points = 0
+ ball_x = paddle_x = 0
+
+ while c.memory[c.pointer] != 99:
+ if c.wants_input:
+ points, ball_x, paddle_x = draw(screen, points)
+ if paddle_x < ball_x:
+ c.input = 1
+ elif paddle_x > ball_x:
+ c.input = -1
+ else:
+ c.input = 0
+ c.step()
+ if c.output is not None:
+ buffer_out.append(c.output)
+ c.output = None
+ if len(buffer_out) == 3:
+ screen[(buffer_out[0], buffer_out[1])] = buffer_out[2]
+ buffer_out = []
+ return c.memory[386]
+
+def visualize(input):
+ def draw(screen, points):
+ ball_x = 0
+ paddle_x = 0
+ min_x = max_x = min_y = max_y = 0
+ for p in screen:
+ min_x = min(min_x, p[0])
+ max_x = max(max_x, p[0])
+ min_y = min(min_y, p[1])
+ max_y = max(max_y, p[1])
+ s = ""
+ for y in range(min_y-1, max_y+1):
+ for x in range(min_x-1, max_x+1):
+ if (x,y) in screen:
+ if (x,y) == (-1,0):
+ points = screen[(x,y)]
+ elif screen[(x,y)] == 0:
+ s += " "
+ elif screen[(x,y)] == 1:
+ s += "\u2588"
+ elif screen[(x,y)] == 2:
+ s += "#"
+ elif screen[(x,y)] == 3:
+ s += "-"
+ paddle_x = x
+ elif screen[(x,y)] == 4:
+ s += "O"
+ ball_x = x
+ else: # (x,y) not in screen
+ s += " "
+ s += "\n"
+ return s, points, ball_x, paddle_x
+
+ c = intcode.Computer([int(x) for x in input[0].split(",")])
+ c.memory[0] = 2
+
+ screen = {}
+ buffer_out = []
+ frame_n = 0
+ points = 0
+ ball_x = paddle_x = 0
+
+ while c.memory[c.pointer] != 99:
+ if c.wants_input:
+ time.sleep(0.005)
+ frame_n += 1
+ frame, points, ball_x, paddle_x = draw(screen, points)
+ print(frame)
+ if paddle_x < ball_x:
+ c.input = 1
+ elif paddle_x > ball_x:
+ c.input = -1
+ else:
+ c.input = 0
+ c.step()
+ if c.output is not None:
+ buffer_out.append(c.output)
+ c.output = None
+ if len(buffer_out) == 3:
+ screen[(buffer_out[0], buffer_out[1])] = buffer_out[2]
+ buffer_out = []
+
+if __name__ == "__main__":
+ import cProfile
+
+ _input = open("../input/13", "r").readlines()
+ cProfile.run("pt1(_input)")
+ cProfile.run("pt2(_input)")
+ print(pt1(_input))
+ print(pt2(_input))
+ if input("Visualize? [Y/n]: ") == "y":
+ visualize(_input)
diff --git a/solutions/py/intcode.py b/solutions/py/intcode.py
index aee72ee..972250e 100644
--- a/solutions/py/intcode.py
+++ b/solutions/py/intcode.py
@@ -23,6 +23,7 @@ class Computer(object):
self.input = None
self.output = None
+ self.wants_input = False
def parse_op(self, op):
#TODO
@@ -70,7 +71,11 @@ class Computer(object):
self.get_param(inst, 1) * self.get_param(inst, 2))
self.pointer += 4
elif inst[0] == IN:
+ if self.input is None:
+ self.wants_input = True
+ return
self.write_param(inst, 1, self.input)
+ self.wants_input = False
self.input = None
self.pointer += 2
elif inst[0] == OUT:
diff --git a/solutions/py/main.py b/solutions/py/main.py
index 526bd00..db4150e 100644
--- a/solutions/py/main.py
+++ b/solutions/py/main.py
@@ -12,8 +12,9 @@ import d09
import d10
import d11
import d12
+import d13
-mods = [d01, d02, d03, d04, d05, d06, d07, d08, d09, d10, d11, d12]
+mods = [d01, d02, d03, d04, d05, d06, d07, d08, d09, d10, d11, d12, d13]
timings = [[0 for _ in range(2)] for _ in range(len(mods))]
clock_type = time.CLOCK_MONOTONIC