parent
62ce050ed2
commit
a012897cee
@ -0,0 +1,5 @@ |
||||
{ |
||||
"python.linting.pylintEnabled": false, |
||||
"python.linting.pycodestyleEnabled": true, |
||||
"python.linting.enabled": true |
||||
} |
||||
@ -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()) |
||||
runner.run(suite()) |
||||
|
||||
@ -1,4 +1,6 @@ |
||||
from .day1 import Day1 |
||||
from .day2 import Day2 |
||||
from .day3 import Day3 |
||||
from .day4 import Day4 |
||||
from .day4 import Day4 |
||||
from .day5 import Day5 |
||||
from .comp import OpcodeComputer |
||||
|
||||
@ -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] |
||||
@ -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() |
||||
@ -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) |
||||
@ -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 |
||||
Loading…
Reference in new issue