Преглед изворни кода

Move intcode interpreter to a separate file, remove some unused imports

Taddeus Kroes пре 6 година
родитељ
комит
16b19642e8
8 измењених фајлова са 53 додато и 231 уклоњено
  1. 2 38
      2019/09_boost.py
  2. 2 38
      2019/11_paintrobot.py
  3. 1 1
      2019/12_jupiter.py
  4. 2 39
      2019/13_arcade.py
  5. 2 39
      2019/15_repair.py
  6. 2 38
      2019/17_ascii.py
  7. 2 38
      2019/19_beam.py
  8. 40 0
      2019/intcode.py

+ 2 - 38
2019/09_boost.py

@@ -1,43 +1,7 @@
 #!/usr/bin/env python3
 import sys
-from operator import add, mul, lt, eq
+from intcode import read_program, run
 
-def run(p, get_input, memsize=0):
-    def decode_param(offset):
-        return p[pc + offset], modes // (10 ** (offset - 1)) % 10
-
-    def pload(offset):
-        param, mode = decode_param(offset)
-        return param if mode == 1 else p[param + relbase * mode // 2]
-
-    def pstore(offset, value):
-        param, mode = decode_param(offset)
-        p[param + relbase * mode // 2] = value
-
-    opmap = {1: add, 2: mul, 7: lt, 8: eq}
-    p = p + [0] * memsize
-    pc = relbase = 0
-
-    while p[pc] != 99:
-        modes, opcode = divmod(p[pc], 100)
-
-        if opcode in (1, 2, 7, 8):
-            pstore(3, opmap[opcode](pload(1), pload(2)))
-            pc += 4
-        elif opcode == 3:
-            pstore(1, get_input())
-            pc += 2
-        elif opcode == 4:
-            yield pload(1)
-            pc += 2
-        elif opcode == 5:
-            pc = pload(2) if pload(1) else pc + 3
-        elif opcode == 6:
-            pc = pload(2) if not pload(1) else pc + 3
-        elif opcode == 9:
-            relbase += pload(1)
-            pc += 2
-
-program = list(map(int, sys.stdin.read().split(',')))
+program = read_program(sys.stdin)
 print(next(run(program, [1].pop, 10000)))
 print(next(run(program, [2].pop, 10000)))

+ 2 - 38
2019/11_paintrobot.py

@@ -1,42 +1,6 @@
 #!/usr/bin/env python3
 import sys
-from operator import add, mul, lt, eq
-
-def run(p, get_input, memsize=0):
-    def decode_param(offset):
-        return p[pc + offset], modes // (10 ** (offset - 1)) % 10
-
-    def pload(offset):
-        param, mode = decode_param(offset)
-        return param if mode == 1 else p[param + relbase * mode // 2]
-
-    def pstore(offset, value):
-        param, mode = decode_param(offset)
-        p[param + relbase * mode // 2] = value
-
-    opmap = {1: add, 2: mul, 7: lt, 8: eq}
-    p = p + [0] * memsize
-    pc = relbase = 0
-
-    while p[pc] != 99:
-        modes, opcode = divmod(p[pc], 100)
-
-        if opcode in (1, 2, 7, 8):
-            pstore(3, opmap[opcode](pload(1), pload(2)))
-            pc += 4
-        elif opcode == 3:
-            pstore(1, get_input())
-            pc += 2
-        elif opcode == 4:
-            yield pload(1)
-            pc += 2
-        elif opcode == 5:
-            pc = pload(2) if pload(1) else pc + 3
-        elif opcode == 6:
-            pc = pload(2) if not pload(1) else pc + 3
-        elif opcode == 9:
-            relbase += pload(1)
-            pc += 2
+from intcode import read_program, run
 
 def paint(firmware, color):
     robot = run(firmware, lambda: color, 1000)
@@ -66,7 +30,7 @@ def draw(coords):
     for row in grid:
         print(''.join(' @'[c] for c in row))
 
-firmware = list(map(int, sys.stdin.read().split(',')))
+firmware = read_program(sys.stdin)
 white, npainted = paint(firmware, 0)
 print(npainted)
 white, npainted = paint(firmware, 1)

+ 1 - 1
2019/12_jupiter.py

@@ -1,5 +1,5 @@
 #!/usr/bin/env python3
-from itertools import chain, combinations, islice
+from itertools import combinations, islice
 from math import gcd
 
 def cmp(a, b):

+ 2 - 39
2019/13_arcade.py

@@ -1,44 +1,8 @@
 #!/usr/bin/env python3
 import sys
 from itertools import islice
-from operator import add, mul, lt, eq
 from time import sleep
-
-def run(p, get_input, memsize=0):
-    def decode_param(offset):
-        return p[pc + offset], modes // (10 ** (offset - 1)) % 10
-
-    def pload(offset):
-        param, mode = decode_param(offset)
-        return param if mode == 1 else p[param + relbase * mode // 2]
-
-    def pstore(offset, value):
-        param, mode = decode_param(offset)
-        p[param + relbase * mode // 2] = value
-
-    opmap = {1: add, 2: mul, 7: lt, 8: eq}
-    p = p + [0] * memsize
-    pc = relbase = 0
-
-    while p[pc] != 99:
-        modes, opcode = divmod(p[pc], 100)
-
-        if opcode in (1, 2, 7, 8):
-            pstore(3, opmap[opcode](pload(1), pload(2)))
-            pc += 4
-        elif opcode == 3:
-            pstore(1, get_input())
-            pc += 2
-        elif opcode == 4:
-            yield pload(1)
-            pc += 2
-        elif opcode == 5:
-            pc = pload(2) if pload(1) else pc + 3
-        elif opcode == 6:
-            pc = pload(2) if not pload(1) else pc + 3
-        elif opcode == 9:
-            relbase += pload(1)
-            pc += 2
+from intcode import read_program, run
 
 def makegrid(game):
     x, y, ident = islice(game, 3)
@@ -65,7 +29,6 @@ def draw(grid, score):
 def play(code, verbose):
     game = run(code, lambda: xball - xpaddle, 1000)
     grid, score = makegrid(game)
-    ypaddle = next(y for y, row in enumerate(grid) if 3 in row)
     xpaddle = xball = 0
     if verbose:
         draw(grid, score)
@@ -87,7 +50,7 @@ def play(code, verbose):
         return score
 
 # part 1
-code = list(map(int, sys.stdin.read().split(',')))
+code = read_program(sys.stdin)
 outp = list(run(code, lambda: 0, 1000))
 print(outp[2::3].count(2))
 

+ 2 - 39
2019/15_repair.py

@@ -1,44 +1,7 @@
 #!/usr/bin/env python3
 import sys
-from collections import defaultdict, deque
-from operator import add, mul, lt, eq
 from time import sleep
-
-def run(p, get_input, memsize=0):
-    def decode_param(offset):
-        return p[pc + offset], modes // (10 ** (offset - 1)) % 10
-
-    def pload(offset):
-        param, mode = decode_param(offset)
-        return param if mode == 1 else p[param + relbase * mode // 2]
-
-    def pstore(offset, value):
-        param, mode = decode_param(offset)
-        p[param + relbase * mode // 2] = value
-
-    opmap = {1: add, 2: mul, 7: lt, 8: eq}
-    p = p + [0] * memsize
-    pc = relbase = 0
-
-    while p[pc] != 99:
-        modes, opcode = divmod(p[pc], 100)
-
-        if opcode in (1, 2, 7, 8):
-            pstore(3, opmap[opcode](pload(1), pload(2)))
-            pc += 4
-        elif opcode == 3:
-            pstore(1, get_input())
-            pc += 2
-        elif opcode == 4:
-            yield pload(1)
-            pc += 2
-        elif opcode == 5:
-            pc = pload(2) if pload(1) else pc + 3
-        elif opcode == 6:
-            pc = pload(2) if not pload(1) else pc + 3
-        elif opcode == 9:
-            relbase += pload(1)
-            pc += 2
+from intcode import read_program, run
 
 NORTH, SOUTH, EAST, WEST = range(1, 5)
 WALL, SPACE, OXYGEN, UNKOWN = range(4)
@@ -111,7 +74,7 @@ def spread_oxygen(mapped):
         draw(mapped, None)
     return minutes
 
-program = list(map(int, sys.stdin.readline().split(',')))
+program = read_program(sys.stdin)
 oxygen_distance, mapped = map_area(program)
 print(oxygen_distance)
 print(spread_oxygen(mapped))

+ 2 - 38
2019/17_ascii.py

@@ -1,43 +1,7 @@
 #!/usr/bin/env python3
 import sys
 from itertools import combinations, islice
-from operator import add, mul, lt, eq
-
-def run(p, get_input, memsize=0):
-    def decode_param(offset):
-        return p[pc + offset], modes // (10 ** (offset - 1)) % 10
-
-    def pload(offset):
-        param, mode = decode_param(offset)
-        return param if mode == 1 else p[param + relbase * mode // 2]
-
-    def pstore(offset, value):
-        param, mode = decode_param(offset)
-        p[param + relbase * mode // 2] = value
-
-    opmap = {1: add, 2: mul, 7: lt, 8: eq}
-    p = p + [0] * memsize
-    pc = relbase = 0
-
-    while p[pc] != 99:
-        modes, opcode = divmod(p[pc], 100)
-
-        if opcode in (1, 2, 7, 8):
-            pstore(3, opmap[opcode](pload(1), pload(2)))
-            pc += 4
-        elif opcode == 3:
-            pstore(1, get_input())
-            pc += 2
-        elif opcode == 4:
-            yield pload(1)
-            pc += 2
-        elif opcode == 5:
-            pc = pload(2) if pload(1) else pc + 3
-        elif opcode == 6:
-            pc = pload(2) if not pload(1) else pc + 3
-        elif opcode == 9:
-            relbase += pload(1)
-            pc += 2
+from intcode import read_program, run
 
 def read_grid(program):
     output = ''.join(map(chr, run(program, None, 10000)))
@@ -139,7 +103,7 @@ def rescue(program, main, subs):
     return output
 
 # part 1
-program = list(map(int, sys.stdin.readline().split(',')))
+program = read_program(sys.stdin)
 grid = read_grid(program)
 print(calibrate(grid))
 

+ 2 - 38
2019/19_beam.py

@@ -2,43 +2,7 @@
 import sys
 from collections import deque
 from itertools import islice
-from operator import add, mul, lt, eq
-
-def run(p, get_input, memsize=0):
-    def decode_param(offset):
-        return p[pc + offset], modes // (10 ** (offset - 1)) % 10
-
-    def pload(offset):
-        param, mode = decode_param(offset)
-        return param if mode == 1 else p[param + relbase * mode // 2]
-
-    def pstore(offset, value):
-        param, mode = decode_param(offset)
-        p[param + relbase * mode // 2] = value
-
-    opmap = {1: add, 2: mul, 7: lt, 8: eq}
-    p = p + [0] * memsize
-    pc = relbase = 0
-
-    while p[pc] != 99:
-        modes, opcode = divmod(p[pc], 100)
-
-        if opcode in (1, 2, 7, 8):
-            pstore(3, opmap[opcode](pload(1), pload(2)))
-            pc += 4
-        elif opcode == 3:
-            pstore(1, get_input())
-            pc += 2
-        elif opcode == 4:
-            yield pload(1)
-            pc += 2
-        elif opcode == 5:
-            pc = pload(2) if pload(1) else pc + 3
-        elif opcode == 6:
-            pc = pload(2) if not pload(1) else pc + 3
-        elif opcode == 9:
-            relbase += pload(1)
-            pc += 2
+from intcode import read_program, run
 
 def deploy_drone(program, x, y):
     return next(run(program, [y, x].pop, 1000))
@@ -70,7 +34,7 @@ def find_box(program, size):
         buf.append((y2, left2, right2))
 
 # part 1
-program = list(map(int, sys.stdin.readline().split(',')))
+program = read_program(sys.stdin)
 #print(sum(sum(row) for row in scan(program, 50, 50)))
 print(sum(min(r, 50) - l for l, r in islice(bounds(program), 50) if l < 50))
 

+ 40 - 0
2019/intcode.py

@@ -0,0 +1,40 @@
+from operator import add, mul, lt, eq
+
+def read_program(f):
+    return list(map(int, f.readline().split(',')))
+
+def run(p, get_input, memsize=0):
+    def decode_param(offset):
+        return p[pc + offset], modes // (10 ** (offset - 1)) % 10
+
+    def pload(offset):
+        param, mode = decode_param(offset)
+        return param if mode == 1 else p[param + relbase * mode // 2]
+
+    def pstore(offset, value):
+        param, mode = decode_param(offset)
+        p[param + relbase * mode // 2] = value
+
+    opmap = {1: add, 2: mul, 7: lt, 8: eq}
+    p = p + [0] * memsize
+    pc = relbase = 0
+
+    while p[pc] != 99:
+        modes, opcode = divmod(p[pc], 100)
+
+        if opcode in (1, 2, 7, 8):
+            pstore(3, opmap[opcode](pload(1), pload(2)))
+            pc += 4
+        elif opcode == 3:
+            pstore(1, get_input())
+            pc += 2
+        elif opcode == 4:
+            yield pload(1)
+            pc += 2
+        elif opcode == 5:
+            pc = pload(2) if pload(1) else pc + 3
+        elif opcode == 6:
+            pc = pload(2) if not pload(1) else pc + 3
+        elif opcode == 9:
+            relbase += pload(1)
+            pc += 2