You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
40 lines
1.2 KiB
40 lines
1.2 KiB
import unittest
|
|
import os
|
|
|
|
class Day2(unittest.TestCase):
|
|
inputfile = os.path.join(os.path.dirname(__file__), "input/day2_input")
|
|
|
|
def process_op(self, pc, mem):
|
|
cmd = mem[pc]
|
|
while cmd != 99:
|
|
if cmd == 1:
|
|
mem[mem[pc+3]] = mem[mem[pc+1]] + mem[mem[pc+2]]
|
|
pc += 4
|
|
elif cmd ==2:
|
|
mem[mem[pc+3]] = mem[mem[pc+1]] * mem[mem[pc+2]]
|
|
pc += 4
|
|
cmd = mem[pc]
|
|
return mem[0]
|
|
|
|
def test_day2a(self):
|
|
with open(self.inputfile) as fp:
|
|
code = [int(k) for k in fp.readline().split(',')]
|
|
code[1] = 12
|
|
code[2] = 2
|
|
self.assertEqual(self.process_op(0, code), 5098658)
|
|
|
|
def test_day2b(self):
|
|
with open(self.inputfile) as fp:
|
|
file_code = [int(k) for k in fp.readline().split(',')]
|
|
for a, b in [(x,y) for x in range(55) for y in range(70)]:
|
|
code = list(file_code)
|
|
code[1] = a
|
|
code[2] = b
|
|
|
|
result = self.process_op(0,code)
|
|
if result == 19690720:
|
|
break
|
|
self.assertEqual(100 * a + b, 5064)
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main() |