浏览代码

Finish 2016

Taddeus Kroes 6 年之前
父节点
当前提交
7610b5dc14
共有 2 个文件被更改,包括 98 次插入0 次删除
  1. 68 0
      2016/25_antenna.py
  2. 30 0
      2016/input/25

+ 68 - 0
2016/25_antenna.py

@@ -0,0 +1,68 @@
+#!/usr/bin/env python3
+import sys
+from itertools import count, islice
+
+def read_program(f):
+    maybe_int = lambda x: int(x) if x.isdigit() or x[0] == '-' else x
+    for line in f:
+        instr = tuple(map(maybe_int, line.split()))
+        yield instr + (None,) * (3 - len(instr))
+
+def check_mul(p, pc):
+    if pc <= len(p) - 5:
+        opcodes, a, b = zip(*p[pc:pc + 5])
+        if a[1] == a[2] and a[3] == a[4] and b[2] == -2 and b[4] == -5:
+            if opcodes == ('inc', 'dec', 'jnz', 'dec', 'jnz'):
+                return a[1], a[3]
+
+def run(p, init):
+    regs = [init, 0, 0, 0]
+    pc = 0
+
+    def load(x):
+        return x if isinstance(x, int) else regs[ord(x) - ord('a')]
+
+    def store(reg, value):
+        regs[ord(reg) - ord('a')] = value
+
+    while pc < len(p):
+        opcode, a, b = p[pc]
+        if opcode == 'cpy':
+            store(b, load(a))
+        elif opcode == 'inc':
+            params = check_mul(p, pc)
+            if params:
+                reg1, reg2 = params
+                increment = load(reg1) * load(reg2)
+                store(reg1, 0)
+                store(reg2, 0)
+                pc += 4
+            else:
+                increment = 1
+            store(a, load(a) + increment)
+        elif opcode == 'dec':
+            store(a, load(a) - 1)
+        elif opcode == 'jnz':
+            if load(a):
+                pc += load(b) - 1
+        elif opcode == 'out':
+            yield load(a)
+        pc += 1
+
+    return regs[0]
+
+def is_clock(signal):
+    expect = 0
+    for x in islice(signal, 8):
+        if x != expect:
+            return False
+        expect = 1 - expect
+    return True
+
+def find_clock(p):
+    for a in count(0):
+        if is_clock(run(p, a)):
+            return a
+
+program = list(read_program(sys.stdin))
+print(find_clock(program))

+ 30 - 0
2016/input/25

@@ -0,0 +1,30 @@
+cpy a d
+cpy 4 c
+cpy 643 b
+inc d
+dec b
+jnz b -2
+dec c
+jnz c -5
+cpy d a
+jnz 0 0
+cpy a b
+cpy 0 a
+cpy 2 c
+jnz b 2
+jnz 1 6
+dec b
+dec c
+jnz c -4
+inc a
+jnz 1 -7
+cpy 2 b
+jnz c 2
+jnz 1 4
+dec b
+dec c
+jnz 1 -4
+jnz 0 0
+out b
+jnz a -19
+jnz 1 -21