about summary refs log tree commit diff
path: root/advent-of-code/day_2.py
diff options
context:
space:
mode:
authorWilliam Carroll <wpcarro@gmail.com>2020-01-15T14·21+0000
committerWilliam Carroll <wpcarro@gmail.com>2020-01-15T14·21+0000
commit456d358cd7b0701b0ca2b8571d61f255d3da1488 (patch)
treecb4aac65aa1c6600f6ef773ffd58cd8579bb5084 /advent-of-code/day_2.py
parentd480b6f08b8a6b32c58e5bbdf2f193432c50fb97 (diff)
Upload my 2019 Advent of Code attempts
Well, unexpectedly (perhaps naively so), I only made it to Day 7. I created
these before I stumbled upon the idea of the mono-repository; otherwise, I like
to think I would have more granular commits introducing this work.
Diffstat (limited to 'advent-of-code/day_2.py')
-rw-r--r--advent-of-code/day_2.py32
1 files changed, 32 insertions, 0 deletions
diff --git a/advent-of-code/day_2.py b/advent-of-code/day_2.py
new file mode 100644
index 000000000000..77774c1bb5ad
--- /dev/null
+++ b/advent-of-code/day_2.py
@@ -0,0 +1,32 @@
+from itertools import product
+
+x = [
+    1, 0, 0, 3, 1, 1, 2, 3, 1, 3, 4, 3, 1, 5, 0, 3, 2, 1, 10, 19, 1, 6, 19, 23,
+    2, 23, 6, 27, 2, 6, 27, 31, 2, 13, 31, 35, 1, 10, 35, 39, 2, 39, 13, 43, 1,
+    43, 13, 47, 1, 6, 47, 51, 1, 10, 51, 55, 2, 55, 6, 59, 1, 5, 59, 63, 2, 9,
+    63, 67, 1, 6, 67, 71, 2, 9, 71, 75, 1, 6, 75, 79, 2, 79, 13, 83, 1, 83, 10,
+    87, 1, 13, 87, 91, 1, 91, 10, 95, 2, 9, 95, 99, 1, 5, 99, 103, 2, 10, 103,
+    107, 1, 107, 2, 111, 1, 111, 5, 0, 99, 2, 14, 0, 0
+]
+
+
+def interpret(i, x):
+    op, a, b, out = x[i + 0], x[i + 1], x[i + 2], x[i + 3]
+    if op == 1:
+        x[out] = x[a] + x[b]
+        return interpret(i + 4, x)
+    elif op == 2:
+        x[out] = x[a] * x[b]
+        return interpret(i + 4, x)
+    elif op == 99:
+        return x
+    else:
+        raise Exception('Unsupported opcode: {}. {}, {}'.format(op, a, b))
+
+
+for a, b in product(range(100), range(100)):
+    y = x[:]
+    y[1] = a
+    y[2] = b
+    if interpret(0, y)[0] == 19690720:
+        print(100 * a + b)