Did a new day

master
Arne Wischer 6 years ago
parent 62ce050ed2
commit a012897cee
  1. 5
      .vscode/settings.json
  2. 18
      advent.py
  3. 2
      days/__init__.py
  4. 41
      days/comp.py
  5. 4
      days/day1.py
  6. 23
      days/day2.py
  7. 4
      days/day3.py
  8. 14
      days/day4.py
  9. 30
      days/day5.py
  10. 1
      days/input/day5_input

@ -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 import unittest
def suite(): def suite():
suite = unittest.TestSuite() suite = unittest.TestSuite()
suite.addTest(days.Day1('test_day1a')) suite.addTest(Day1('test_day1a'))
suite.addTest(days.Day1('test_day1b')) suite.addTest(Day1('test_day1b'))
suite.addTest(days.Day2('test_day2a')) suite.addTest(Day2('test_day2a'))
suite.addTest(days.Day2('test_day2b')) suite.addTest(Day2('test_day2b'))
suite.addTest(days.Day4('test_day4a')) suite.addTest(Day4('test_day4a'))
suite.addTest(days.Day4('test_day4b')) suite.addTest(Day4('test_day4b'))
suite.addTest(Day5('test_day5a'))
suite.addTest(Day5('test_day5b'))
return suite return suite
if __name__ == "__main__": if __name__ == "__main__":
runner = unittest.TextTestRunner(verbosity=2) runner = unittest.TextTestRunner(verbosity=2)
runner.run(suite()) runner.run(suite())

@ -2,3 +2,5 @@ from .day1 import Day1
from .day2 import Day2 from .day2 import Day2
from .day3 import Day3 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,6 +1,7 @@
import unittest import unittest
import os import os
class Day1(unittest.TestCase): class Day1(unittest.TestCase):
inputfile = os.path.join(os.path.dirname(__file__), "input/day1_input") 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)) sumfuel += self.calc_fuel(int(line))
line = fp.readline() line = fp.readline()
self.assertEqual(sumfuel, 5101025) self.assertEqual(sumfuel, 5101025)
if __name__ == "__main__":
unittest.main()

@ -1,40 +1,27 @@
import unittest import unittest
import os import os
from .comp import OpcodeComputer
class Day2(unittest.TestCase): class Day2(unittest.TestCase):
inputfile = os.path.join(os.path.dirname(__file__), "input/day2_input") 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): def test_day2a(self):
with open(self.inputfile) as fp: with open(self.inputfile) as fp:
code = [int(k) for k in fp.readline().split(',')] code = [int(k) for k in fp.readline().split(',')]
code[1] = 12 code[1] = 12
code[2] = 2 code[2] = 2
self.assertEqual(self.process_op(0, code), 5098658) self.assertEqual(OpcodeComputer().process_op(code), 5098658)
def test_day2b(self): def test_day2b(self):
with open(self.inputfile) as fp: with open(self.inputfile) as fp:
file_code = [int(k) for k in fp.readline().split(',')] 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 = list(file_code)
code[1] = a code[1] = a
code[2] = b code[2] = b
result = self.process_op(0,code) result = OpcodeComputer().process_op(code)
if result == 19690720: if result == 19690720:
break break
self.assertEqual(100 * a + b, 5064) self.assertEqual(100 * a + b, 5064)
if __name__ == "__main__":
unittest.main()

@ -1,6 +1,7 @@
import unittest import unittest
import os import os
class Day3(unittest.TestCase): class Day3(unittest.TestCase):
inputfile = os.path.join(os.path.dirname(__file__), "input/day3_input") 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: with open(self.inputfile) as fp:
steps = fp.readline().split(',') steps = fp.readline().split(',')
pass pass
if __name__ == "__main__":
unittest.main()

@ -2,10 +2,11 @@ import unittest
import os import os
from collections import Counter from collections import Counter
class Day4(unittest.TestCase): class Day4(unittest.TestCase):
def check_num(self, num): def check_num(self, num):
numarr = [int(d) for d in (str(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]: if numarr[p-1] > numarr[p]:
return False return False
c = Counter(str(num)) c = Counter(str(num))
@ -13,7 +14,7 @@ class Day4(unittest.TestCase):
def check_num_grp(self, num): def check_num_grp(self, num):
numarr = [int(d) for d in (str(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]: if numarr[p-1] > numarr[p]:
return False return False
c = Counter(str(num)) c = Counter(str(num))
@ -22,16 +23,13 @@ class Day4(unittest.TestCase):
def test_day4a(self): def test_day4a(self):
counter = 0 counter = 0
for num in range(168630, 718099): for num in range(168630, 718099):
if self.check_num(num) == True: if self.check_num(num) is True:
counter += 1 counter += 1
self.assertEqual(counter, 1686) self.assertEqual(counter, 1686)
def test_day4b(self): def test_day4b(self):
counter = 0 counter = 0
for num in range(168630,718099): for num in range(168630, 718099):
if self.check_num_grp(num) == True: if self.check_num_grp(num) is True:
counter += 1 counter += 1
self.assertEqual(counter, 1145) self.assertEqual(counter, 1145)
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…
Cancel
Save