2019 Day 13 Part 2; intcode as class; intcode_test to assure functionality; changed old code to adapt intcode computer as object

This commit is contained in:
Akumatic
2020-12-03 23:45:28 +01:00
parent 592513f6b7
commit 0d748eae52
8 changed files with 317 additions and 212 deletions

View File

@ -5,44 +5,24 @@
import sys, os
sys.path.insert(1, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import intcode
import intcode, intcode_test
def readFile() -> list:
with open(f"{__file__.rstrip('code.py')}input.txt", "r") as f:
return [int(num) for num in f.readline().split(",")]
def part1(vals: list) -> int:
return intcode.getOutput(vals.copy(), input=1)
def part1(pc: intcode.Computer) -> int:
pc.reset(input=1)
pc.run()
return pc.data[0]
def part2(vals: list) -> int:
return intcode.getOutput(vals.copy(), input=5)
def test():
assert intcode.parseCode(1001) == (1, 0, 1, 0)
assert intcode.getOutput([1,0,0,0,99]) == 2
assert intcode.getOutput([1,1,1,4,99,5,6,0,99]) == 30
assert intcode.getOutput([3,0,4,0,99], input=42) == 42
assert intcode.getOutput([1101,100,-1,4,0], getVals=True) == [1101,100,-1,4,99]
assert intcode.getOutput([3,9,8,9,10,9,4,9,99,-1,8], input=0) == 0
assert intcode.getOutput([3,9,8,9,10,9,4,9,99,-1,8], input=8) == 1
assert intcode.getOutput([3,9,7,9,10,9,4,9,99,-1,8], input=0) == 1
assert intcode.getOutput([3,9,7,9,10,9,4,9,99,-1,8], input=8) == 0
assert intcode.getOutput([3,12,6,12,15,1,13,14,13,4,13,99,-1,0,1,9], input=0) == 0
assert intcode.getOutput([3,12,6,12,15,1,13,14,13,4,13,99,-1,0,1,9], input=1) == 1
assert intcode.getOutput([3,3,1105,-1,9,1101,0,0,12,4,12,99,1], input=0) == 0
assert intcode.getOutput([3,3,1105,-1,9,1101,0,0,12,4,12,99,1], input=1) == 1
assert intcode.getOutput([3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31,
1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104,999,1105,1,46,
1101,1000,1,20,4,20,1105,1,46,98,99], input=0) == 999
assert intcode.getOutput([3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31,
1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104,999,1105,1,46,
1101,1000,1,20,4,20,1105,1,46,98,99], input=8) == 1000
assert intcode.getOutput([3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31,
1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104,999,1105,1,46,
1101,1000,1,20,4,20,1105,1,46,98,99], input=9) == 1001
def part2(pc: intcode.Computer) -> int:
pc.reset(input=5)
pc.run()
return pc.data[0]
if __name__ == "__main__":
test()
vals = readFile()
print(f"Part 1: {part1(vals)}")
print(f"Part 2: {part2(vals)}")
intcode_test.test_05()
pc = intcode.Computer(readFile())
print(f"Part 1: {part1(pc)}")
print(f"Part 2: {part2(pc)}")