Simplify scaffold code

This commit is contained in:
Anders Englöf Ytterström 2024-11-29 15:55:28 +01:00
parent cdd5f63be4
commit 63a7ccd0e2
7 changed files with 27 additions and 140 deletions

View file

@ -2,9 +2,9 @@ import sys
from pathlib import Path from pathlib import Path
def headline(n, title): def headline(n):
"""Print day number and name, followed by a ruler. Used by the answer decorator""" """Print day number and name, followed by a ruler. Used by the answer decorator"""
print(f"\n--- Day {n}: {title} ---\n") print(f"\n--- Day {n} ---\n")
year = 2016 year = 2016
@ -31,19 +31,6 @@ if day_no and name:
f""" f"""
from output import answer # , matrix, D, DD, ADJ, ints, mhd, mdbg, vdbg from output import answer # , matrix, D, DD, ADJ, ints, mhd, mdbg, vdbg
n = {day_no}
title = "{name}"
@answer(1, "Answer is {{}}")
def part_1(presolved):
return presolved[0]
@answer(2, "Actually, answer is {{}}")
def part_2(presolved):
return presolved[1]
def solve(data): def solve(data):
return 1, 2 return 1, 2
@ -63,10 +50,10 @@ if __name__ == "__main__":
# inp = f.read().strip() # inp = f.read().strip()
# uncomment to do initial data processing shared by part 1-2 # uncomment to do initial data processing shared by part 1-2
inp = solve(inp) p1, p2 = solve(inp)
a = part_1(inp) print(p1)
# b = part_2(inp) # print(p2)
# uncomment and replace 0 with actual output to refactor code # uncomment and replace 0 with actual output to refactor code
# and ensure nonbreaking changes # and ensure nonbreaking changes
@ -97,23 +84,25 @@ for i in [str(n).zfill(2) for n in range(1, 26)]:
"output.day_{}".format(i), "output.day_{}".format(i),
globals(), globals(),
locals(), locals(),
["n", "title", "part_1", "part_2"], ["solve"],
0, 0,
) )
with open(f"./input/{i}.txt", "r") as f: with open(f"./input/{i}.txt", "r") as f:
data = f.read().strip() data = f.read().strip()
headline(day.n, day.title) headline(i)
try: try:
data = day.presolve(data) data = day.presolve(data)
except AttributeError: except AttributeError:
pass pass
try: try:
data = day.solve(data) p1, p2 = day.solve(data)
except AttributeError: except AttributeError:
pass pass
if day.part_1(data, decorate=True): if p1:
print(f" 1. {p1}")
stars += 1 stars += 1
if day.part_2(data, decorate=True): if p2:
print(f" 2. {p2}")
stars += 1 stars += 1
except IOError: except IOError:
pass pass

View file

@ -2,24 +2,9 @@ import re
from collections import Counter, deque from collections import Counter, deque
from itertools import combinations from itertools import combinations
from output import answer # , matrix, D, DD, ADJ, ints, mhd, mdbg, vdbg
n = 11
title = "Radioisotope Thermoelectric Generators"
D = {1: [2], 2: [1, 3], 3: [2, 4], 4: [3]} D = {1: [2], 2: [1, 3], 3: [2, 4], 4: [3]}
@answer(1, "To transport all objects to the 4th floor, {} steps are required")
def part_1(presolved):
return presolved[0]
@answer(2, "With the additonal objects to transport, {} steps are required")
def part_2(presolved):
return presolved[1]
def solve(data): def solve(data):
def parse(row): def parse(row):
return sorted( return sorted(
@ -145,10 +130,7 @@ if __name__ == "__main__":
with open("./input/11.txt", "r") as f: with open("./input/11.txt", "r") as f:
inp = f.read().strip() inp = f.read().strip()
inp = solve(inp) p1, p2 = solve(inp)
a = part_1(inp) print(p1)
b = part_2(inp) print(p2)
assert a == 37
assert b == 61

View file

@ -1,21 +1,3 @@
from output import answer # , matrix, D, DD, ADJ, ints, mhd, mdbg, vdbg
n = 12
title = "Leonardo's Monorail"
@answer(1, "Value of registry a will be {} on exit")
def part_1(presolved):
return presolved[0]
@answer(
2, "If register c is initialized with 1, value of registry a will be {} on exit"
)
def part_2(presolved):
return presolved[1]
def solve(data): def solve(data):
p = data.splitlines() p = data.splitlines()
pl = len(p) pl = len(p)
@ -64,10 +46,7 @@ if __name__ == "__main__":
with open("./input/12.txt", "r") as f: with open("./input/12.txt", "r") as f:
inp = f.read().strip() inp = f.read().strip()
inp = solve(inp) p1, p2 = solve(inp)
a = part_1(inp) print(p1)
b = part_2(inp) print(p2)
assert a == 318009
assert b == 9227663

View file

@ -46,9 +46,6 @@ if __name__ == "__main__":
with open("./input/13.txt", "r") as f: with open("./input/13.txt", "r") as f:
inp = f.read().strip() inp = f.read().strip()
t, _ = solve("10", (7, 4))
assert t == 11
p1, p2 = solve(inp) p1, p2 = solve(inp)
print(p1) print(p1)

View file

@ -2,21 +2,6 @@ import functools
import re import re
from hashlib import md5 from hashlib import md5
from output import answer # , matrix, D, DD, ADJ, ints, mhd, mdbg, vdbg
n = 14
title = "One-Time Pad"
@answer(1, "64th key is at index {}")
def part_1(presolved):
return presolved[0]
@answer(2, "64th key is at index {} using key stretching")
def part_2(presolved):
return presolved[1]
def solve(s): def solve(s):
p1 = run(s) p1 = run(s)
@ -66,10 +51,7 @@ if __name__ == "__main__":
with open("./input/14.txt", "r") as f: with open("./input/14.txt", "r") as f:
inp = f.read().strip() inp = f.read().strip()
inp = solve(inp) p1, p2 = solve(inp)
a = part_1(inp) print(p1)
b = part_2(inp) print(p2)
assert a == 18626
assert b == 20092

View file

@ -1,20 +1,5 @@
import re import re
from output import answer # , matrix, D, DD, ADJ, ints, mhd, mdbg, vdbg
n = 15
title = "Timing is Everything"
@answer(1, "First time for capsule-giving button press is {}")
def part_1(presolved):
return presolved[0]
@answer(2, "With the additional disc, first time for capsule-giving button press is {}")
def part_2(presolved):
return presolved[1]
def solve(data): def solve(data):
M = [ M = [
@ -36,18 +21,10 @@ def wait_and_press(M):
if __name__ == "__main__": if __name__ == "__main__":
inp = """
Disc #1 has 5 positions; at time=0, it is at position 4.
Disc #2 has 2 positions; at time=0, it is at position 1.
""".strip()
with open("./input/15.txt", "r") as f: with open("./input/15.txt", "r") as f:
inp = f.read().strip() inp = f.read().strip()
inp = solve(inp) p1, p2 = solve(inp)
a = part_1(inp) print(p1)
b = part_2(inp) print(p2)
assert a == 122318
assert b == 3208583

View file

@ -1,19 +1,3 @@
from output import answer # , matrix, D, DD, ADJ, ints, mhd, mdbg, vdbg
n = 16
title = "Dragon Checksum"
@answer(1, "The checksum to the state to fill first disc is {}")
def part_1(presolved):
return presolved[0]
@answer(2, "The checksum to the state to fill second disc is {}")
def part_2(presolved):
return presolved[1]
def solve(data): def solve(data):
p12 = [] p12 = []
for DS in [272, 35651584]: for DS in [272, 35651584]:
@ -34,10 +18,7 @@ if __name__ == "__main__":
with open("./input/16.txt", "r") as f: with open("./input/16.txt", "r") as f:
inp = f.read().strip() inp = f.read().strip()
inp = solve(inp) p1, p2 = solve(inp)
a = part_1(inp) print(p1)
b = part_2(inp) print(p2)
assert a == "10011010010010010"
assert b == "10101011110100011"