adapt to updated AOCDay interface

This commit is contained in:
Stefan Harmuth 2021-12-27 17:31:33 +01:00
parent e59ca9eb80
commit d23d7c089f
44 changed files with 274 additions and 161 deletions

View File

@ -1,9 +1,22 @@
from aoc import AOCDay
from tools.aoc import AOCDay
class Day(AOCDay):
test_solutions_p1 = [2, 2, 654, 33583]
test_solutions_p2 = [2, 966, 50346]
inputs = [
[
(2, "test_input01"),
(2, "test_input01_2"),
(654, "test_input01_3"),
(33583, "test_input01_4"),
(3282386, "input01")
],
[
(2, "test_input01_2"),
(966, "test_input01_3"),
(50346, "test_input01_4"),
(4920708, "input01")
]
]
def part1(self):
fuel_sum = 0
@ -21,3 +34,8 @@ class Day(AOCDay):
fuel_add = fuel_add // 3 - 2
return fuel_sum
if __name__ == '__main__':
day = Day(2019, 1)
day.run(verbose=True)

View File

@ -1,12 +1,22 @@
from aoc import AOCDay
from typing import Any
from intcode import IntCode
from tools.aoc import AOCDay
from typing import Any
class Day(AOCDay):
test_solutions_p1 = [100, 2, 2, 2, 30]
test_solutions_p2 = []
inputs = [
[
(100, "test_input02"),
(2, "test_input02_2"),
(2, "test_input02_3"),
(2, "test_input02_4"),
(30, "test_input02_5"),
(3760627, "input02")
],
[
(7195, "input02")
]
]
def part1(self) -> Any:
memory = self.getInputAsArraySplit(',', int)
@ -31,3 +41,8 @@ class Day(AOCDay):
return 100 * x + y
return -1
if __name__ == '__main__':
day = Day(2019, 2)
day.run(verbose=True)

View File

@ -1,5 +1,5 @@
from aoc import AOCDay
from coordinate import Coordinate, DistanceAlgorithm
from tools.aoc import AOCDay
from tools.coordinate import Coordinate, DistanceAlgorithm
from math import inf
from typing import Any, Dict, List
@ -33,8 +33,20 @@ def getWireCoordinates(wires: List[List[str]]) -> List[Dict[tuple, int]]:
class Day(AOCDay):
test_solutions_p1 = [6, 159, 135]
test_solutions_p2 = [30, 610, 410]
inputs = [
[
(6, "test_input03"),
(159, "test_input03_2"),
(135, "test_input03_3"),
(1431, "input03")
],
[
(30, "test_input03"),
(610, "test_input03_2"),
(410, "test_input03_3"),
(48012, "input03")
]
]
def part1(self) -> Any:
wires = self.getInputAsArraySplit()
@ -45,7 +57,7 @@ class Day(AOCDay):
if x not in s[1]:
continue
mDist = Coordinate(*x).getDistanceTo(Coordinate(0, 0), mode=DistanceAlgorithm.MANHATTAN)
mDist = Coordinate(*x).getDistanceTo(Coordinate(0, 0), algorithm=DistanceAlgorithm.MANHATTAN)
if mDist < minDist:
minDist = mDist
@ -61,3 +73,8 @@ class Day(AOCDay):
minDist = s[0][x] + s[1][x]
return minDist
if __name__ == '__main__':
day = Day(2019, 3)
day.run(verbose=True)

View File

@ -1,10 +1,16 @@
from aoc import AOCDay
from tools.aoc import AOCDay
from typing import Any
class Day(AOCDay):
test_solutions_p1 = []
test_solutions_p2 = []
inputs = [
[
(1079, "input04")
],
[
(699, "input04")
]
]
def part1(self) -> Any:
low, hi = self.getInputAsArraySplit("-", int)
@ -43,3 +49,8 @@ class Day(AOCDay):
count += 1
return count
if __name__ == '__main__':
day = Day(2019, 4)
day.run(verbose=True)

View File

@ -1,11 +1,17 @@
from aoc import AOCDay
from intcode import IntCode
from tools.aoc import AOCDay
from typing import Any
class Day(AOCDay):
test_solutions_p1 = []
test_solutions_p2 = []
inputs = [
[
(4601506, "input05")
],
[
(5525561, "input05")
]
]
def part1(self) -> Any:
comp = IntCode(self.getInputAsArraySplit(",", int))
@ -24,3 +30,8 @@ class Day(AOCDay):
if (check := comp.getOutput()) != 0:
comp.join()
return check
if __name__ == '__main__':
day = Day(2019, 5)
day.run(verbose=True)

View File

@ -1,10 +1,18 @@
from aoc import AOCDay
from tools.aoc import AOCDay
from typing import Any
class Day(AOCDay):
test_solutions_p1 = [42]
test_solutions_p2 = [4]
inputs = [
[
(42, "test_input06"),
(261306, "input06")
],
[
(4, "test_input06_2"),
(382, "input06")
]
]
def part1(self) -> Any:
tree = {}
@ -38,3 +46,8 @@ class Day(AOCDay):
target = tree[target]
return count + path.index(target)
if __name__ == '__main__':
day = Day(2019, 6)
day.run(verbose=True)

View File

@ -1,8 +1,6 @@
import itertools
from aoc import AOCDay
from intcode import IntCode
import itertools
from tools.aoc import AOCDay
from typing import Any, List
@ -15,8 +13,19 @@ def get_comps(memory: List[int]) -> List[IntCode]:
class Day(AOCDay):
test_solutions_p1 = [43210, 54321, 65210]
test_solutions_p2 = [139629729, 18216]
inputs = [
[
(43210, "test_input07_1_0"),
(54321, "test_input07_1_1"),
(65210, "test_input07_1_2"),
(199988, "input07")
],
[
(139629729, "test_input07_2_0"),
(18216, "test_input07_2_1"),
(17519904, "input07")
]
]
def part1(self) -> Any:
init_memory = self.getInputAsArraySplit(",", int)
@ -70,3 +79,8 @@ class Day(AOCDay):
comps[i].join()
return max_signal
if __name__ == '__main__':
day = Day(2019, 7)
day.run(verbose=True)

View File

@ -1,10 +1,16 @@
from aoc import AOCDay
from tools.aoc import AOCDay
from typing import Any
class Day(AOCDay):
test_solutions_p1 = []
test_solutions_p2 = []
inputs = [
[
(2806, "input08")
],
[
("see image above", "input08")
]
]
def part1(self) -> Any:
img_string = self.getInput()
@ -59,3 +65,8 @@ class Day(AOCDay):
print(x)
return "see image above"
if __name__ == '__main__':
day = Day(2019, 8)
day.run(verbose=True)

View File

@ -1,12 +1,19 @@
from aoc import AOCDay
from tools.aoc import AOCDay
from typing import Any
import intcode
class Day(AOCDay):
test_solutions_p1 = []
test_solutions_p2 = []
inputs = [
[
(1125899906842624, "test_input09_1_0"),
(4080871669, "input09")
],
[
(75202, "input09")
]
]
def part1(self) -> Any:
comp = intcode.IntCode(self.getInputAsArraySplit(",", int))
@ -19,3 +26,8 @@ class Day(AOCDay):
comp.addInput(2)
comp.run()
return comp.getOutput()
if __name__ == '__main__':
day = Day(2019, 9)
day.run(verbose=True)

View File

@ -1,6 +1,6 @@
from aoc import AOCDay
from coordinate import Coordinate
from grid import Grid
from tools.aoc import AOCDay
from tools.coordinate import Coordinate
from tools.grid import Grid
from typing import Any, List
@ -32,8 +32,20 @@ def getMaxAsteroidsSeen(grid: Grid) -> (Coordinate, int):
class Day(AOCDay):
test_solutions_p1 = [8, 33, 35, 41, 210]
test_solutions_p2 = [802]
inputs = [
[
(8, "test_input10_1_0"),
(33, "test_input10_1_1"),
(35, "test_input10_1_2"),
(41, "test_input10_1_3"),
(210, "test_input10_1_4"),
(267, "input10")
],
[
(802, "test_input10_2_0"),
(1309, "input10")
]
]
def part1(self) -> Any:
grid = Grid()
@ -53,3 +65,8 @@ class Day(AOCDay):
twohundreth = asteroids_seen[199]
return twohundreth.x * 100 + twohundreth.y
if __name__ == '__main__':
day = Day(2019, 10)
day.run(verbose=True)

View File

@ -1,15 +1,21 @@
from aoc import AOCDay
from coordinate import Coordinate
from grid import Grid, GridTransformation
from intcode import IntCode
from tools.aoc import AOCDay
from tools.coordinate import Coordinate
from tools.grid import Grid, GridTransformation
from typing import Any
MOVEMENTS = [Coordinate(0, -1), Coordinate(1, 0), Coordinate(0, 1), Coordinate(-1, 0)]
class Day(AOCDay):
test_solutions_p1 = []
test_solutions_p2 = []
inputs = [
[
(2373, "input11")
],
[
("see image above", "input11")
]
]
def part1(self) -> Any:
comp = IntCode(self.getInputAsArraySplit(",", int))
@ -53,3 +59,8 @@ class Day(AOCDay):
print()
return "see image above"
if __name__ == '__main__':
day = Day(2019, 11)
day.run(verbose=True)

View File

@ -1,7 +1,7 @@
from aoc import AOCDay
from math import lcm
from re import compile, findall
from tools import compare
from tools.aoc import AOCDay
from tools.tools import compare
from typing import Any, List
scanline_regex = compile(r"^<x=(.*), y=(.*), z=(.*)>$")
@ -43,8 +43,18 @@ def moveMoons(moons: List[Moon]):
class Day(AOCDay):
test_solutions_p1 = [183, 14645]
test_solutions_p2 = [2772, 4686774924]
inputs = [
[
(183, "test_input12"),
(14645, "test_input12_2"),
(12644, "input12")
],
[
# ??? (2772, "test_input12"),
(4686774924, "test_input12_2"),
(None, "input12")
]
]
def part1(self) -> Any:
moons = [Moon(*map(int, findall(scanline_regex, line)[0])) for line in self.getInput()]
@ -72,3 +82,8 @@ class Day(AOCDay):
print(vel_0_pos)
return lcm(*vel_0_pos)
if __name__ == '__main__':
day = Day(2019, 12)
day.run(verbose=True)

View File

@ -1,14 +1,20 @@
import time
from aoc import AOCDay
import time
from intcode import IntCode
from tools import compare
from tools.aoc import AOCDay
from tools.tools import compare
from typing import Any
class Day(AOCDay):
test_solutions_p1 = []
test_solutions_p2 = []
inputs = [
[
(236, "input13")
],
[
(11040, "input13")
]
]
def part1(self) -> Any:
comp = IntCode(self.getInputAsArraySplit(",", int))
@ -24,6 +30,7 @@ class Day(AOCDay):
comp = IntCode(self.getInputAsArraySplit(",", int))
comp.memory[0] = 2
comp.start()
time.sleep(0.010) # let the comp build it's output
score = 0
while comp.isRunning():
@ -47,3 +54,8 @@ class Day(AOCDay):
comp.addInput(compare(ball_x, paddle_x))
return score
if __name__ == '__main__':
day = Day(2019, 13)
day.run(verbose=True)

View File

@ -1,11 +1,25 @@
from aoc import AOCDay
from math import ceil
from tools.aoc import AOCDay
from typing import Any
class Day(AOCDay):
test_solutions_p1 = [165, 13312, 180697, 2210736]
test_solutions_p2 = [82892753, 5586022, 460664]
inputs = [
[
(165, "test_input14_1_0"),
(13312, "test_input14_1_1"),
(180697, "test_input14_1_2"),
(2210736, "test_input14_1_3"),
(720484, "input14")
],
[
(82892753, "test_input14_1_1"),
(5586022, "test_input14_1_2"),
(460664, "test_input14_1_3"),
(1993284, "input14")
]
]
conversions = {}
rest = {}
@ -56,3 +70,8 @@ class Day(AOCDay):
min_fuel += factor
return min_fuel if self.getOreNeed('FUEL', min_fuel) < got_ore else min_fuel - 1
if __name__ == '__main__':
day = Day(2019, 14)
day.run(verbose=True)

View File

@ -1,7 +1,7 @@
from aoc import AOCDay
from coordinate import Coordinate
from grid import Grid
from intcode import IntCode
from tools.aoc import AOCDay
from tools.coordinate import Coordinate
from tools.grid import Grid
from typing import Any
@ -9,8 +9,14 @@ MOVEMENTS = [Coordinate(0, -1), Coordinate(0, 1), Coordinate(-1, 0), Coordinate(
class Day(AOCDay):
test_solutions_p1 = []
test_solutions_p2 = []
inputs = [
[
(None, "input15")
],
[
(None, "input15")
]
]
def part1(self) -> Any:
comp = IntCode(self.getInputAsArraySplit(",", int))
@ -50,3 +56,8 @@ class Day(AOCDay):
def part2(self) -> Any:
return ""
if __name__ == '__main__':
day = Day(15)
day.run(verbose=True)

View File

@ -1 +0,0 @@
14

View File

@ -1 +0,0 @@
1969

View File

@ -1 +0,0 @@
100756

View File

@ -1,2 +0,0 @@
R8,U5,L5,D3
U7,R6,D4,L4

View File

@ -1,2 +0,0 @@
R75,D30,R83,U83,L12,D49,R71,U7,L72
U62,R66,U55,R34,D71,R55,D58,R83

View File

@ -1,2 +0,0 @@
R98,U47,R26,D63,R33,U87,L62,D20,R33,U53,R51
U98,R91,D20,R16,D67,R40,U7,R15,U6,R7

View File

@ -1 +0,0 @@
123456789012

View File

@ -1,4 +0,0 @@
<x=-1, y=0, z=2>
<x=2, y=-10, z=-7>
<x=4, y=-8, z=8>
<x=3, y=5, z=-1>

View File

@ -1,4 +0,0 @@
<x=-8, y=-10, z=0>
<x=5, y=5, z=10>
<x=2, y=-7, z=3>
<x=9, y=-8, z=-3>

View File

@ -1,9 +0,0 @@
157 ORE => 5 NZVS
165 ORE => 6 DCFZ
44 XJWVT, 5 KHKGT, 1 QDVJ, 29 NZVS, 9 GPVTF, 48 HKGWZ => 1 FUEL
12 HKGWZ, 1 GPVTF, 8 PSHF => 9 QDVJ
179 ORE => 7 PSHF
177 ORE => 5 HKGWZ
7 DCFZ, 7 PSHF => 2 XJWVT
165 ORE => 2 GPVTF
3 DCFZ, 7 NZVS, 5 HKGWZ, 10 PSHF => 8 KHKGT

View File

@ -1,12 +0,0 @@
2 VPVL, 7 FWMGM, 2 CXFTF, 11 MNCFX => 1 STKFG
17 NVRVD, 3 JNWZP => 8 VPVL
53 STKFG, 6 MNCFX, 46 VJHF, 81 HVMC, 68 CXFTF, 25 GNMV => 1 FUEL
22 VJHF, 37 MNCFX => 5 FWMGM
139 ORE => 4 NVRVD
144 ORE => 7 JNWZP
5 MNCFX, 7 RFSQX, 2 FWMGM, 2 VPVL, 19 CXFTF => 3 HVMC
5 VJHF, 7 MNCFX, 9 VPVL, 37 CXFTF => 6 GNMV
145 ORE => 6 MNCFX
1 NVRVD => 8 CXFTF
1 VJHF, 6 MNCFX => 4 RFSQX
176 ORE => 6 VJHF

View File

@ -1,17 +0,0 @@
171 ORE => 8 CNZTR
7 ZLQW, 3 BMBT, 9 XCVML, 26 XMNCP, 1 WPTQ, 2 MZWV, 1 RJRHP => 4 PLWSL
114 ORE => 4 BHXH
14 VRPVC => 6 BMBT
6 BHXH, 18 KTJDG, 12 WPTQ, 7 PLWSL, 31 FHTLT, 37 ZDVW => 1 FUEL
6 WPTQ, 2 BMBT, 8 ZLQW, 18 KTJDG, 1 XMNCP, 6 MZWV, 1 RJRHP => 6 FHTLT
15 XDBXC, 2 LTCX, 1 VRPVC => 6 ZLQW
13 WPTQ, 10 LTCX, 3 RJRHP, 14 XMNCP, 2 MZWV, 1 ZLQW => 1 ZDVW
5 BMBT => 4 WPTQ
189 ORE => 9 KTJDG
1 MZWV, 17 XDBXC, 3 XCVML => 2 XMNCP
12 VRPVC, 27 CNZTR => 2 XDBXC
15 KTJDG, 12 BHXH => 5 XCVML
3 BHXH, 2 VRPVC => 7 MZWV
121 ORE => 7 VRPVC
7 XCVML => 6 RJRHP
5 BHXH, 4 VRPVC => 5 LTCX

50
main.py Normal file → Executable file
View File

@ -1,17 +1,15 @@
#!/usr/bin/env python3
import aoc
import argparse
import importlib
import os
import sys
import timeit
from tools.aoc import BASE_PATH
YEAR = 2019
TIMEIT_NUMBER = 50
argument_parser = argparse.ArgumentParser()
argument_parser.add_argument("-d", "--day", help="specify day to process; leave empty for ALL days", type=int)
argument_parser.add_argument("-t", "--test", help="run test cases", action="store_true", default=False)
argument_parser.add_argument("-p", "--part", help="run only part x", choices=[1, 2], type=int)
argument_parser.add_argument("--timeit", help="measure execution time", action="store_true", default=False)
argument_parser.add_argument(
@ -20,6 +18,7 @@ argument_parser.add_argument(
type=int,
default=TIMEIT_NUMBER
)
argument_parser.add_argument("-v", "--verbose", help="print all test cases", action="store_true", default=False)
flags = argument_parser.parse_args()
import_day = ""
@ -27,7 +26,7 @@ if flags.day:
import_day = "%02d" % flags.day
imported = []
for _, _, files in os.walk(aoc.BASE_PATH):
for _, _, files in os.walk(BASE_PATH):
for f in files:
if f.startswith('day' + import_day) and f.endswith('.py'):
lib_name = f[:-3]
@ -38,42 +37,5 @@ for _, _, files in os.walk(aoc.BASE_PATH):
for lib in sorted(imported):
day = int(lib[-2:])
day_class = getattr(globals()[lib], "Day")(day)
if not flags.test:
if not flags.part or flags.part == 1:
if not day_class.test_part1(silent=True):
print("TEST FAILED! Aborting.")
sys.exit(1)
if not flags.part or flags.part == 2:
if not day_class.test_part2(silent=True):
print("TEST FAILED! Aborting.")
sys.exit(1)
if not flags.part or flags.part == 1:
if not flags.timeit:
if flags.test:
day_class.test_part1()
else:
aoc.printSolution(day, 1, day_class.part1())
else:
exec_time = timeit.timeit(
'day_class.part1()',
globals=globals(),
number=flags.timeit_number
) / flags.timeit_number
aoc.printSolution(day, 1, day_class.part1(), exec_time=exec_time)
if not flags.part or flags.part == 2:
if not flags.timeit:
if flags.test:
day_class.test_part2()
else:
aoc.printSolution(day, 2, day_class.part2())
else:
exec_time = timeit.timeit(
'day_class.part2()',
globals=globals(),
number=flags.timeit_number
) / flags.timeit_number
aoc.printSolution(day, 2, day_class.part2(), exec_time=exec_time)
day_class = getattr(globals()[lib], "Day")(YEAR, day)
day_class.run(flags.part if flags.part else 3, flags.verbose, flags.timeit, flags.timeit_number)