浏览代码

Solve 2016 days 23 and 24

Taddeus Kroes 6 年之前
父节点
当前提交
fcd54bc12c
共有 4 个文件被更改,包括 178 次插入0 次删除
  1. 62 0
      2016/23_safe.py
  2. 49 0
      2016/24_hvac.py
  3. 26 0
      2016/input/23
  4. 41 0
      2016/input/24

+ 62 - 0
2016/23_safe.py

@@ -0,0 +1,62 @@
+#!/usr/bin/env python3
+import sys
+
+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
+    toggle_opcode = {'inc': 'dec', 'dec': 'inc', 'tgl': 'inc',
+                     'cpy': 'jnz', 'jnz': 'cpy'}
+
+    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]
+        try:
+            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 == 'tgl':
+                offset = load(a)
+                other_opcode, other_a, other_b = p[pc + offset]
+                p[pc + offset] = toggle_opcode[other_opcode], other_a, other_b
+        except IndexError:
+            pass
+        pc += 1
+
+    return regs[0]
+
+program = list(read_program(sys.stdin))
+print(run(program.copy(), 7))
+print(run(program, 12))

+ 49 - 0
2016/24_hvac.py

@@ -0,0 +1,49 @@
+#!/usr/bin/env python3
+import sys
+from collections import deque
+from itertools import chain, permutations
+
+def read_grid(f):
+    rows = [line.rstrip() for line in f]
+    return list(chain.from_iterable(rows)), len(rows[0])
+
+def shortest_paths(grid, w):
+    def bfs(start):
+        dists = [0] * nlocs
+        work = deque([(start, 0)])
+        explored = {start}
+        while work:
+            loc, dist = work.popleft()
+
+            node = grid[loc]
+            if node.isdigit():
+                dists[int(node)] = dist
+
+            for nb in (loc - 1, loc + 1, loc - w, loc + w):
+                if grid[nb] != '#' and nb not in explored:
+                    explored.add(nb)
+                    work.append((nb, dist + 1))
+        return dists
+
+    nlocs = max(int(x) for x in grid if x.isdigit()) + 1
+    return [bfs(grid.index(str(i))) for i in range(nlocs)]
+
+def visit_steps(dists, back):
+    dists = shortest_paths(grid, w)
+    best = 100000000
+    for path in permutations(range(1, len(dists))):
+        total = 0
+        prev = 0
+        for loc in path:
+            total += dists[prev][loc]
+            prev = loc
+        if back:
+            total += dists[prev][0]
+        best = min(best, total)
+    return best
+
+
+grid, w = read_grid(sys.stdin)
+dists = shortest_paths(grid, w)
+print(visit_steps(dists, False))
+print(visit_steps(dists, True))

+ 26 - 0
2016/input/23

@@ -0,0 +1,26 @@
+cpy a b
+dec b
+cpy a d
+cpy 0 a
+cpy b c
+inc a
+dec c
+jnz c -2
+dec d
+jnz d -5
+dec b
+cpy b c
+cpy c d
+dec d
+inc c
+jnz d -2
+tgl c
+cpy -16 c
+jnz 1 c
+cpy 75 c
+jnz 72 d
+inc a
+inc d
+jnz d -2
+inc c
+jnz c -5

+ 41 - 0
2016/input/24

@@ -0,0 +1,41 @@
+#########################################################################################################################################################################################
+#.#.......#.#.#.....#.#.......#.................#.......#.#.....#.....#...#...#.......#...#...........#.#.....#.............#.........#.............#.........#.....#.#.............#...#
+#.#####.#.#.#.#.#.#.#.#.#.###.#.#.###.#.#.###.###.#.#.#.#.#.#####.#.#.###.#.#.###.#.#.###.###.#.###.###.###.#.#.###.#.#.#.#.#.#.###.#.###.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.###.#####.#.###.#
+#.....#.............#...#....6#.....#.....#.#...#.....#...#.........#.......#...#.#.....#.....#.#...#...#.....#.#.......#.........#...#...#.#.#.......#.........#.....#...#.#.#.#.....#.#
+#####.#.###.###.#.#.#.#####.#.#####.#.#####.#.#######.#.#.#.#.#.#####.#####.#.#########.#.###.#.#.#.#.#.#.#.#######.#.#.#####.###.###.###.#.#.###.#.#.#.#.#.#.#.#.###########.#.#.#####.#
+#.....#.......#.#.....#.......#.#...#.#.#.#...........#.....#.#.#...#.#...#.#.#.....#...#.....#.....#...............#.......#.#.........#.#...........#.....#...#.......#.#...#.#.....#.#
+###.#.#.#.#####.#####.#.#.#.#.#.#.#.#.#.#.#########.#.#########.#.#.###.###.###.###.#.#.#.#.#.#.#.#######.#.#.###.#.###.#.#.#.#.#.#.#######.#.#.#.###.#.#.#.###.###.#.#.#.#.#.###.#.#####
+#...#...#.#.....#.............#...#...#...#.#.......#.#.#...#...#.#.#...#.....#.#.#.....#.#...#.#.......#.#.#...#.......#.....#.....#.......#...#...#.#...#.....#.#.......#.#.#...#...#.#
+#.#.#####.#.###.###.#.#########.#.#.#.###.#.#.#.#.#.#.#.#.#.#.#.#.#.###.#.#.#.###.#.#.#.#.#######.#.#.###.#.###.###.#.#.#.#####.#####.#####.#.#.#.#.#.###.#.#.###.#.#####.#.#####.#.###.#
+#...#.#...#.....#.#.........#.#.....#...#.....#...#...#...#...#...#.........#.......#...#.#.....#...#.#...#.#.......#.........#...#.#...#...#2..#.#.#.#...#.......#...#.#...........#...#
+#.###.#####.#####.#.#.###.###.#.###.#.#.#.#.###.#.#.###.#.#.#.###.#.#.#####.#.#####.#.###.#.#.#.#.###.#.#.#.###.#.#.#.#####.#.#.#.#.#.###.#####.#.#.#.#.#.#####.###.#.#.#.#.#.#.#####.#.#
+#...#.#.#4#.#.........#.......#...........#.#...#...#...#.........#.......#.....#...#...#.#...#...#...#.#...#.........#.....#...#.....#.........#...#...............#.#.#...#.........#.#
+#######.#.#.###.###.###.###.#.#####.#####.#.#.###.#.#.#########.#####.#.#####.#.#.#.###.#.#.###.#.#.#.#.#.#.#.#####.#.#####.#.#.#.#.#.#.#.#.#.###.#.#######.#####.#.#.#.#.#.#.###.#.#####
+#.........#.#.....#.....#...................#.....#...#.........#.....#.#.....#.#...........#...#...#.........#...#...#.....#...#.#.#.#.....#.......#.#3....#.......#.....#.#.#.....#...#
+#.#####.###.#.###.#####.#.###.###.###.#.#####.#.#.###.###.#.###.#.#.#.###.#.###.#.###.#.#####.#.###.###.#######.#.#.#.#.###.#.#.#.#########.#.###.###.###.#.#.###.#.#######.###.#.#.#.#.#
+#...#...#.........#...#.......#...#...#...#.........#.....#.....#.....#...#.......#...#...#...#.#.#.#...#.................#.............................#...#.....#.....#.#.....#.....#.#
+#.#.#.#.#####.###.#.#.#.#.###.###.###.#.###.#.#.#####.###.#.###.#.#.###.#.#.#####.###.#.#.###.#.#.#.###.#.#.###.#####.#.###.###.#.#.#.#.#.#.###.#.#.#.#.#.###.###.#.#.#.#.#####.#####.#.#
+#...#.......#...#.....#...#.#.#.#.#...#.#...#.....#.#.#.....#.....#.......#.#.......#.........#.....#...#...#...#.#.........#.#.#.....#.#...#...#.....#...#.......#.........#.....#...#.#
+###.#.#.#.#.#####.###.#####.#.#.#.#.###.#####.###.#.#.#.#####.#.#.#.#.#.#.###.#.#.###.#.#.###.#.#.###.#.#.###.###.#.#.#.#.#.#.#.#.#####.#.#.#.###.#.#.#.###.###.###.###.###.#.#.#####.#.#
+#7#.......#...#...#...#...#.......#.#.........#.#.......#.....#...#.....#.#...#...#...#.#.#...#.#.......#.....#.#.#.....#...#...#...#.....#.....#.#.#.#.#...#...#...#.#.......#.....#...#
+#.#.#.#.#.#.#######.#.#.#.#####.#.###.###.#.###.#.###.#.###.#.#######.###.#.#.#.#.#####.###.###.#####.#.#####.#.###.#####.#.#.#.###.#.#######.#.#.###.#.###.#####.#.#.#.#.###.###.###.#.#
+#.......#...#.#.....#.#.....#.#...#.......#...#.......#.#.....#.........#.#.#.....#.........#.#.#...............#...#.....#...#.#...#.......#.#...........#...#.#.#.#.#.#.....#...#.....#
+#.#.#.#######.#.###.#.#.###.#.#.#.#.#####.#.#.#.#.#.#.#.#.#.#.#.#######.#####.###.#.#####.###.#####.#.#.###.#.###.#.#.###.#.###.#.#.#.#.#.###.###.#.###.#.#.#.#.#.#.###.#.#.#.#.#.#.#.#.#
+#.....#.#...#.#.....#...........#...#.#.....#.#.......#.....#.#...#...........#.....#.........#...#.#.#.#.#.#.....#...#.#.............#.....#.#.#.#.#.#.#.#.#.....#.#.#.#...........#...#
+#####.#.###.#.#.#.#.#.###.#.#.#.###.#.#.#######.#####.###.#.#.#.#.#.#.#.#.#.###.#.#####.#.#######.#.#####.#.#.#########.#########.#.#.###.###.#.#####.#.###.###.#.#.#.#.#.#.#.#######.#.#
+#.....#...#.#.......#...........#...#.#.....#.#.......#.#...#.#...#.......#...#.......#...#.........#...#.........................#...#...#...#.#.#...#.#.........#...#...#...#...#...#.#
+#.#.#.#.#.#.#####.#.#.#########.#.#####.#.#.#.#.#.###.#.#.#.#.###.#.#.#.#.#.#.###.#.#######.###.#.#.#.#######.#.###.#####.###.#.#.#####.#.#.#####.#######.#.#.###.###.#.#.#.#.#.###.###.#
+#.#...#...........#.#.......#...#.#.............#.......#...#.....#.#.....#...#.....#.......#...#.#.#...#...#.#...#.#.....#...#...#...#.....#.....#...#.....#.......#.....#.#.#.#0#.....#
+###.#.#.#.#.#.#######.#.###.###.#.#.###.#.#######.#.###.#########.#.#.#.#.###.#.###.###########.#.###.###.#.#.###.#.#.###.#.#.###.###.#.###.#.###########.###.#.###.#####.#.#.#.#.#.#.#.#
+#.#...#.#.#...#.........#...#.......#.#.................#.#...........#.....#.......#.....#.#.#.#.#...#.....#...#...#.#.........#.#.....#.#...#.....#.....#.#...#.#...#.......#.#.....#.#
+#.###.#.#.#.#.#.#.#.#.#.#.#.#.###.#.#.#.#.#####.#.#####.#.###.#.#.###.#.###.###.###.#.#####.#.#.#######.#.#####.###.###.#######.###.#####.#.#.#.#.#.#.#.#.#.###.#.#.#.#.#.###.#.#.#.#.#.#
+#.#...#.........#.#...#...........#.#.....#5........#...#.....#.......#.#...#.......#.........#.......#.........#.#.#.......#...#.#...#.#.#.....#...#.#.#.#.......#.#.....#...#.#.....#.#
+###.###.#.#####.###.###.###.#.#.###.#.#.#####.###.#.#.#.#.###.#####.#.#.#.#.#.#.#.###.#.###.#.###.#.#.#.#.###.#.#.#.#######.#.###.#.#.#.#.###.###.#.#.#.#.#.#######.#.#.#.#####.###.#.#.#
+#...#...........#...#...#.#.......#.....#.#.#.#...............#.....#.....#...#.#.#...#.#...#.#.....#...#.#...#...#.#.#.......#...#.#...#.......#...#.......#.......#.#...#.#.....#.....#
+#.#.#.###.#.#.#######.###.#####.#.#######.#.###.#######.#.###.###.#.#.#####.#.###.#.#.#.#.###.###.#.#.#.###.#.#.###.#.###.#########.###.#.#.#####.#.#####.###.#.#.#.###.#.#.#####.#######
+#...#.................#.....#.....#...#...#.#...#...#.....#.......#.#...#...#.....#.....#.........#...#.....#.........#...#.....#...#.......#1..#.#.#.#...#...#.#.......#.#...#.......#.#
+#.#.#.#####.#.#.###.###.###.#####.#.#.#####.#.#.#.#####.#.###.###.#.###.#.#.#.###.#.#.#.#.#.#.#.#.#.#.#.###.#######.#.#.#####.###.#######.#.#.#.#.#.#.#.###.#.#.#.#####.#.#######.#.#.#.#
+#.....#.....#.#...............#.#...#.....#.................#...#.#...#.#...#.....#.#...#.........#...........#.#.......#.....#.#.....#.....#.......#...........#...#.....#.#.......#.#.#
+#.#.#.#.#.#.#.#######.###.###.#.#.#.#.#.#.###.###.#.#.###.#.#.#.#.#.#.#####.#.#.#.#.###.#.#.#######.#.#.#.###.#.#.#.#.###.#.#.#.#.#.#.#.#####.#.#.#.#.#####.#####.###.###.#.#.#.#.#.#.#.#
+#...#.#...#.#.......#.......#.....#...#.......#...#...#...#.....#.#.....#...#.#...#.....#.#.........#.#.#.....#.....#...#.........#.#.#.......#.........#...#.....#.#...#.#...#.....#...#
+#########################################################################################################################################################################################