From 63a7ccd0e27f8839a5cd313b1c305198bd1246df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20Engl=C3=B6f=20Ytterstr=C3=B6m?= Date: Fri, 29 Nov 2024 15:55:28 +0100 Subject: [PATCH] Simplify scaffold code --- 2016-python2/aoc.py | 35 ++++++++++++----------------------- 2016-python2/output/day_11.py | 24 +++--------------------- 2016-python2/output/day_12.py | 27 +++------------------------ 2016-python2/output/day_13.py | 3 --- 2016-python2/output/day_14.py | 24 +++--------------------- 2016-python2/output/day_15.py | 29 +++-------------------------- 2016-python2/output/day_16.py | 25 +++---------------------- 7 files changed, 27 insertions(+), 140 deletions(-) diff --git a/2016-python2/aoc.py b/2016-python2/aoc.py index 517712d..a7f6b48 100644 --- a/2016-python2/aoc.py +++ b/2016-python2/aoc.py @@ -2,9 +2,9 @@ import sys 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(f"\n--- Day {n}: {title} ---\n") + print(f"\n--- Day {n} ---\n") year = 2016 @@ -31,19 +31,6 @@ if day_no and name: f""" 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): return 1, 2 @@ -63,10 +50,10 @@ if __name__ == "__main__": # inp = f.read().strip() # uncomment to do initial data processing shared by part 1-2 - inp = solve(inp) + p1, p2 = solve(inp) - a = part_1(inp) - # b = part_2(inp) + print(p1) + # print(p2) # uncomment and replace 0 with actual output to refactor code # 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), globals(), locals(), - ["n", "title", "part_1", "part_2"], + ["solve"], 0, ) with open(f"./input/{i}.txt", "r") as f: data = f.read().strip() - headline(day.n, day.title) + headline(i) try: data = day.presolve(data) except AttributeError: pass try: - data = day.solve(data) + p1, p2 = day.solve(data) except AttributeError: pass - if day.part_1(data, decorate=True): + if p1: + print(f" 1. {p1}") stars += 1 - if day.part_2(data, decorate=True): + if p2: + print(f" 2. {p2}") stars += 1 except IOError: pass diff --git a/2016-python2/output/day_11.py b/2016-python2/output/day_11.py index 249a5f2..19dfdd2 100644 --- a/2016-python2/output/day_11.py +++ b/2016-python2/output/day_11.py @@ -2,24 +2,9 @@ import re from collections import Counter, deque 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]} -@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 parse(row): return sorted( @@ -145,10 +130,7 @@ if __name__ == "__main__": with open("./input/11.txt", "r") as f: inp = f.read().strip() - inp = solve(inp) + p1, p2 = solve(inp) - a = part_1(inp) - b = part_2(inp) - - assert a == 37 - assert b == 61 + print(p1) + print(p2) diff --git a/2016-python2/output/day_12.py b/2016-python2/output/day_12.py index 5f27240..442dae7 100644 --- a/2016-python2/output/day_12.py +++ b/2016-python2/output/day_12.py @@ -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): p = data.splitlines() pl = len(p) @@ -64,10 +46,7 @@ if __name__ == "__main__": with open("./input/12.txt", "r") as f: inp = f.read().strip() - inp = solve(inp) + p1, p2 = solve(inp) - a = part_1(inp) - b = part_2(inp) - - assert a == 318009 - assert b == 9227663 + print(p1) + print(p2) diff --git a/2016-python2/output/day_13.py b/2016-python2/output/day_13.py index 64e9b08..3e8e0e1 100644 --- a/2016-python2/output/day_13.py +++ b/2016-python2/output/day_13.py @@ -46,9 +46,6 @@ if __name__ == "__main__": with open("./input/13.txt", "r") as f: inp = f.read().strip() - t, _ = solve("10", (7, 4)) - assert t == 11 - p1, p2 = solve(inp) print(p1) diff --git a/2016-python2/output/day_14.py b/2016-python2/output/day_14.py index 01e96ae..3d31d0d 100644 --- a/2016-python2/output/day_14.py +++ b/2016-python2/output/day_14.py @@ -2,21 +2,6 @@ import functools import re 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): p1 = run(s) @@ -66,10 +51,7 @@ if __name__ == "__main__": with open("./input/14.txt", "r") as f: inp = f.read().strip() - inp = solve(inp) + p1, p2 = solve(inp) - a = part_1(inp) - b = part_2(inp) - - assert a == 18626 - assert b == 20092 + print(p1) + print(p2) diff --git a/2016-python2/output/day_15.py b/2016-python2/output/day_15.py index 27354f9..5640ac9 100644 --- a/2016-python2/output/day_15.py +++ b/2016-python2/output/day_15.py @@ -1,20 +1,5 @@ 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): M = [ @@ -36,18 +21,10 @@ def wait_and_press(M): 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: inp = f.read().strip() - inp = solve(inp) + p1, p2 = solve(inp) - a = part_1(inp) - b = part_2(inp) - - assert a == 122318 - assert b == 3208583 + print(p1) + print(p2) diff --git a/2016-python2/output/day_16.py b/2016-python2/output/day_16.py index 3a26bcd..f218db1 100644 --- a/2016-python2/output/day_16.py +++ b/2016-python2/output/day_16.py @@ -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): p12 = [] for DS in [272, 35651584]: @@ -34,10 +18,7 @@ if __name__ == "__main__": with open("./input/16.txt", "r") as f: inp = f.read().strip() - inp = solve(inp) + p1, p2 = solve(inp) - a = part_1(inp) - b = part_2(inp) - - assert a == "10011010010010010" - assert b == "10101011110100011" + print(p1) + print(p2)