From a012897cee431098e9e99fab0b4c2be205282363 Mon Sep 17 00:00:00 2001 From: Arne Wischer Date: Thu, 5 Dec 2019 18:33:55 +0100 Subject: [PATCH] Did a new day --- .vscode/settings.json | 5 +++++ advent.py | 20 ++++++++++++-------- days/__init__.py | 4 +++- days/comp.py | 41 +++++++++++++++++++++++++++++++++++++++++ days/day1.py | 4 +--- days/day2.py | 25 ++++++------------------- days/day3.py | 4 +--- days/day4.py | 14 ++++++-------- days/day5.py | 30 ++++++++++++++++++++++++++++++ days/input/day5_input | 1 + 10 files changed, 106 insertions(+), 42 deletions(-) create mode 100644 .vscode/settings.json create mode 100644 days/comp.py create mode 100644 days/day5.py create mode 100644 days/input/day5_input diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..b07a863 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "python.linting.pylintEnabled": false, + "python.linting.pycodestyleEnabled": true, + "python.linting.enabled": true +} \ No newline at end of file diff --git a/advent.py b/advent.py index d8a5607..6373a5e 100644 --- a/advent.py +++ b/advent.py @@ -1,16 +1,20 @@ -import days +from days import * import unittest + def suite(): suite = unittest.TestSuite() - suite.addTest(days.Day1('test_day1a')) - suite.addTest(days.Day1('test_day1b')) - suite.addTest(days.Day2('test_day2a')) - suite.addTest(days.Day2('test_day2b')) - suite.addTest(days.Day4('test_day4a')) - suite.addTest(days.Day4('test_day4b')) + suite.addTest(Day1('test_day1a')) + suite.addTest(Day1('test_day1b')) + suite.addTest(Day2('test_day2a')) + suite.addTest(Day2('test_day2b')) + suite.addTest(Day4('test_day4a')) + suite.addTest(Day4('test_day4b')) + suite.addTest(Day5('test_day5a')) + suite.addTest(Day5('test_day5b')) return suite + if __name__ == "__main__": runner = unittest.TextTestRunner(verbosity=2) - runner.run(suite()) \ No newline at end of file + runner.run(suite()) diff --git a/days/__init__.py b/days/__init__.py index 9c8e6b1..f23ed7b 100644 --- a/days/__init__.py +++ b/days/__init__.py @@ -1,4 +1,6 @@ from .day1 import Day1 from .day2 import Day2 from .day3 import Day3 -from .day4 import Day4 \ No newline at end of file +from .day4 import Day4 +from .day5 import Day5 +from .comp import OpcodeComputer diff --git a/days/comp.py b/days/comp.py new file mode 100644 index 0000000..32a282b --- /dev/null +++ b/days/comp.py @@ -0,0 +1,41 @@ +class OpcodeComputer(): + def parse_param(self, pc, mem): + i = str(mem[pc]) + a, b, c = (0, 0, 0) + try: + a = mem[pc+1] if i[-3:-4:-1] == '1' else mem[mem[pc+1]] + b = mem[pc+2] if i[-4:-5:-1] == '1' else mem[mem[pc+2]] + c = mem[pc+3] if i[-5:-6:-1] == '1' else mem[mem[pc+3]] + except IndexError: + pass + return (int(i[-2:]), a, b, c) + + def process_op(self, mem): + pc = 0 + cmd = mem[pc] + while cmd != 99: + cmd, a, b, c = self.parse_param(pc, mem) + if cmd == 1: + mem[mem[pc+3]] = a + b + pc += 4 + elif cmd == 2: + mem[mem[pc+3]] = a * b + pc += 4 + elif cmd == 3: + mem[mem[pc+1]] = int(input()) + pc += 2 + elif cmd == 4: + print(str(a)) + pc += 2 + elif cmd == 5: + pc = b if a != 0 else pc + 3 + elif cmd == 6: + pc = b if a == 0 else pc + 3 + elif cmd == 7: + mem[mem[pc+3]] = 1 if a < b else 0 + pc += 4 + elif cmd == 8: + mem[mem[pc+3]] = 1 if a == b else 0 + pc += 4 + cmd = mem[pc] + return mem[0] diff --git a/days/day1.py b/days/day1.py index 888b265..6c1e8ce 100644 --- a/days/day1.py +++ b/days/day1.py @@ -1,6 +1,7 @@ import unittest import os + class Day1(unittest.TestCase): inputfile = os.path.join(os.path.dirname(__file__), "input/day1_input") @@ -23,6 +24,3 @@ class Day1(unittest.TestCase): sumfuel += self.calc_fuel(int(line)) line = fp.readline() self.assertEqual(sumfuel, 5101025) - -if __name__ == "__main__": - unittest.main() \ No newline at end of file diff --git a/days/day2.py b/days/day2.py index 81361c4..f8e1780 100644 --- a/days/day2.py +++ b/days/day2.py @@ -1,40 +1,27 @@ import unittest import os +from .comp import OpcodeComputer + 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) + self.assertEqual(OpcodeComputer().process_op(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)]: + 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) + + result = OpcodeComputer().process_op(code) if result == 19690720: break self.assertEqual(100 * a + b, 5064) - -if __name__ == "__main__": - unittest.main() \ No newline at end of file diff --git a/days/day3.py b/days/day3.py index 5b0f486..d308f28 100644 --- a/days/day3.py +++ b/days/day3.py @@ -1,6 +1,7 @@ import unittest import os + class Day3(unittest.TestCase): inputfile = os.path.join(os.path.dirname(__file__), "input/day3_input") @@ -8,6 +9,3 @@ class Day3(unittest.TestCase): with open(self.inputfile) as fp: steps = fp.readline().split(',') pass - -if __name__ == "__main__": - unittest.main() \ No newline at end of file diff --git a/days/day4.py b/days/day4.py index db947b6..b8377c3 100644 --- a/days/day4.py +++ b/days/day4.py @@ -2,10 +2,11 @@ import unittest import os from collections import Counter + class Day4(unittest.TestCase): def check_num(self, num): numarr = [int(d) for d in (str(num))] - for p in range(1,6): + for p in range(1, 6): if numarr[p-1] > numarr[p]: return False c = Counter(str(num)) @@ -13,7 +14,7 @@ class Day4(unittest.TestCase): def check_num_grp(self, num): numarr = [int(d) for d in (str(num))] - for p in range(1,6): + for p in range(1, 6): if numarr[p-1] > numarr[p]: return False c = Counter(str(num)) @@ -22,16 +23,13 @@ class Day4(unittest.TestCase): def test_day4a(self): counter = 0 for num in range(168630, 718099): - if self.check_num(num) == True: + if self.check_num(num) is True: counter += 1 self.assertEqual(counter, 1686) def test_day4b(self): counter = 0 - for num in range(168630,718099): - if self.check_num_grp(num) == True: + for num in range(168630, 718099): + if self.check_num_grp(num) is True: counter += 1 self.assertEqual(counter, 1145) - -if __name__ == "__main__": - unittest.main() \ No newline at end of file diff --git a/days/day5.py b/days/day5.py new file mode 100644 index 0000000..6ebb709 --- /dev/null +++ b/days/day5.py @@ -0,0 +1,30 @@ +import unittest +import os +from . import comp + + +class Day5(unittest.TestCase): + def get_code(self): + inputfile = os.path.join(os.path.dirname(__file__), "input/day5_input") + with open(inputfile) as fp: + return [int(k) for k in fp.readline().split(',')] + + def test_day5a(self): + output_val = [] + + sut = comp.OpcodeComputer() + comp.input = lambda: 1 + comp.print = lambda s: output_val.append(s) + + sut.process_op(self.get_code()) + self.assertEqual(int(output_val[-1]), 8332629) + + def test_day5b(self): + output_val = [] + + sut = comp.OpcodeComputer() + comp.input = lambda: 5 + comp.print = lambda s: output_val.append(s) + + sut.process_op(self.get_code()) + self.assertEqual(int(output_val[-1]), 8805067) diff --git a/days/input/day5_input b/days/input/day5_input new file mode 100644 index 0000000..f7e1734 --- /dev/null +++ b/days/input/day5_input @@ -0,0 +1 @@ +3,225,1,225,6,6,1100,1,238,225,104,0,1101,82,10,225,101,94,44,224,101,-165,224,224,4,224,1002,223,8,223,101,3,224,224,1,224,223,223,1102,35,77,225,1102,28,71,225,1102,16,36,225,102,51,196,224,101,-3468,224,224,4,224,102,8,223,223,1001,224,7,224,1,223,224,223,1001,48,21,224,101,-57,224,224,4,224,1002,223,8,223,101,6,224,224,1,223,224,223,2,188,40,224,1001,224,-5390,224,4,224,1002,223,8,223,101,2,224,224,1,224,223,223,1101,9,32,224,101,-41,224,224,4,224,1002,223,8,223,1001,224,2,224,1,223,224,223,1102,66,70,225,1002,191,28,224,101,-868,224,224,4,224,102,8,223,223,101,5,224,224,1,224,223,223,1,14,140,224,101,-80,224,224,4,224,1002,223,8,223,101,2,224,224,1,224,223,223,1102,79,70,225,1101,31,65,225,1101,11,68,225,1102,20,32,224,101,-640,224,224,4,224,1002,223,8,223,1001,224,5,224,1,224,223,223,4,223,99,0,0,0,677,0,0,0,0,0,0,0,0,0,0,0,1105,0,99999,1105,227,247,1105,1,99999,1005,227,99999,1005,0,256,1105,1,99999,1106,227,99999,1106,0,265,1105,1,99999,1006,0,99999,1006,227,274,1105,1,99999,1105,1,280,1105,1,99999,1,225,225,225,1101,294,0,0,105,1,0,1105,1,99999,1106,0,300,1105,1,99999,1,225,225,225,1101,314,0,0,106,0,0,1105,1,99999,8,226,226,224,1002,223,2,223,1006,224,329,101,1,223,223,1008,677,677,224,102,2,223,223,1006,224,344,101,1,223,223,1107,226,677,224,102,2,223,223,1005,224,359,101,1,223,223,1008,226,226,224,1002,223,2,223,1006,224,374,1001,223,1,223,1108,677,226,224,1002,223,2,223,1006,224,389,1001,223,1,223,7,677,226,224,1002,223,2,223,1006,224,404,101,1,223,223,7,226,226,224,1002,223,2,223,1005,224,419,101,1,223,223,8,226,677,224,1002,223,2,223,1006,224,434,1001,223,1,223,7,226,677,224,1002,223,2,223,1006,224,449,1001,223,1,223,107,226,677,224,1002,223,2,223,1005,224,464,1001,223,1,223,1007,677,677,224,102,2,223,223,1005,224,479,101,1,223,223,1007,226,226,224,102,2,223,223,1005,224,494,1001,223,1,223,1108,226,677,224,102,2,223,223,1005,224,509,101,1,223,223,1008,677,226,224,102,2,223,223,1005,224,524,1001,223,1,223,1007,677,226,224,102,2,223,223,1005,224,539,101,1,223,223,1108,226,226,224,1002,223,2,223,1005,224,554,101,1,223,223,108,226,226,224,102,2,223,223,1005,224,569,101,1,223,223,108,677,677,224,102,2,223,223,1005,224,584,101,1,223,223,1107,226,226,224,1002,223,2,223,1006,224,599,101,1,223,223,8,677,226,224,1002,223,2,223,1006,224,614,1001,223,1,223,108,677,226,224,102,2,223,223,1006,224,629,1001,223,1,223,1107,677,226,224,1002,223,2,223,1006,224,644,1001,223,1,223,107,677,677,224,102,2,223,223,1005,224,659,101,1,223,223,107,226,226,224,102,2,223,223,1006,224,674,1001,223,1,223,4,223,99,226 \ No newline at end of file