From 62ce050ed223ea48b6bf92b2b85d36784230346e Mon Sep 17 00:00:00 2001 From: Arne Wischer Date: Thu, 5 Dec 2019 14:03:58 +0100 Subject: [PATCH] Re-Implement Day2 --- days/day2.py | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/days/day2.py b/days/day2.py index 901ba2a..81361c4 100644 --- a/days/day2.py +++ b/days/day2.py @@ -4,11 +4,37 @@ 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): - self.assertTrue(1 == 1) + 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): - self.assertTrue(1 == 1) + 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() \ No newline at end of file