Browse Source

Day 12

master
Chris Smith 7 years ago
parent
commit
0fb2f5eecc
2 changed files with 56 additions and 0 deletions
  1. 33
    0
      12.py
  2. 23
    0
      12.txt

+ 33
- 0
12.py View File

@@ -0,0 +1,33 @@
1
+#!/usr/bin/python3
2
+
3
+with open('12.txt', 'r') as file:
4
+    instr = list(map(str.split, map(str.strip, file.readlines())))
5
+
6
+def value(x):
7
+    try:
8
+        return int(x)
9
+    except ValueError:
10
+        return registers[x]
11
+
12
+def cpy(args): registers[args[1]] = value(args[0])
13
+def inc(args): registers[args[0]] += 1
14
+def dec(args): registers[args[0]] -= 1
15
+def jnz(args): global pc; pc += (0 if value(args[0]) == 0 else value(args[1]) - 1)
16
+
17
+def run():
18
+    global pc
19
+    while pc < len(instr):
20
+        globals()[instr[pc][0]](instr[pc][1:])
21
+        pc += 1
22
+
23
+registers = {'a': 0, 'b': 0, 'c': 0, 'd': 0}
24
+pc = 0
25
+run()
26
+
27
+print("Stage 1: %s" % registers['a'])
28
+
29
+registers = {'a': 0, 'b': 0, 'c': 1, 'd': 0}
30
+pc = 0
31
+run()
32
+
33
+print("Stage 2: %s" % registers['a'])

+ 23
- 0
12.txt View File

@@ -0,0 +1,23 @@
1
+cpy 1 a
2
+cpy 1 b
3
+cpy 26 d
4
+jnz c 2
5
+jnz 1 5
6
+cpy 7 c
7
+inc d
8
+dec c
9
+jnz c -2
10
+cpy a c
11
+inc a
12
+dec b
13
+jnz b -2
14
+cpy c b
15
+dec d
16
+jnz d -6
17
+cpy 13 c
18
+cpy 14 d
19
+inc a
20
+dec d
21
+jnz d -2
22
+dec c
23
+jnz c -5

Loading…
Cancel
Save