Turns out the initial trial of using Manhattan distance was required to solve part 2, since the BFS implementation is impossible to scale up from 2 robots to 25. Recursion and memoization makes the execution time acceptable. Line 53 makes all the difference here. The order of `<|v|^|>` matters when constructing a sequence. Many hours was spent trying to find the correct priority. For the example input, especially 456A and 379A were volatile.
115 lines
2.8 KiB
Python
115 lines
2.8 KiB
Python
import sys
|
|
from pathlib import Path
|
|
|
|
|
|
def headline(n):
|
|
"""Print day number and name, followed by a ruler. Used by the answer decorator"""
|
|
print(f"\nDay {int(n)} - https://adventofcode.com/{year}/day/{int(n)}\n")
|
|
|
|
|
|
year = 2024
|
|
|
|
try:
|
|
_, day_no, *name = sys.argv
|
|
except ValueError:
|
|
day_no = None
|
|
name = None
|
|
|
|
Path("./input").mkdir(parents=True, exist_ok=True)
|
|
Path("./output").mkdir(parents=True, exist_ok=True)
|
|
|
|
if day_no and name:
|
|
name = " ".join(name)
|
|
padded_no = day_no.zfill(2)
|
|
with open("output/day_{}.py".format(padded_no), "w") as s:
|
|
s.write(
|
|
f"""
|
|
import re
|
|
from collections import deque, Counter, defaultdict
|
|
from heapq import heappop, heappush
|
|
from itertools import compress, combinations, chain, permutations
|
|
|
|
from output import matrix, D, DD, ADJ, ints, mhd, mdbg, vdbg, cw, ccw, bk
|
|
|
|
|
|
def solve(data):
|
|
p1 = None
|
|
p2 = None
|
|
return p1, p2
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import os
|
|
|
|
# use dummy data
|
|
inp = \"\"\"
|
|
replace me
|
|
\"\"\".strip()
|
|
|
|
# uncomment to instead use stdin
|
|
# import sys; inp = sys.stdin.read().strip()
|
|
|
|
# uncomment to use AoC provided puzzle input
|
|
# with open("./input/{padded_no}.txt", "r") as f:
|
|
# inp = f.read().strip()
|
|
|
|
# uncomment to do initial data processing shared by part 1-2
|
|
p1, p2 = solve(inp)
|
|
|
|
print(p1)
|
|
os.system(f"echo {{p1}} | wl-copy")
|
|
# print(p2)
|
|
# os.system(f"echo {{p2}} | wl-copy")
|
|
|
|
# uncomment and replace 0 with actual output to refactor code
|
|
# and ensure nonbreaking changes
|
|
# assert p1 == 0
|
|
# assert p2 == 0
|
|
""".strip()
|
|
+ "\n"
|
|
)
|
|
exit(0)
|
|
|
|
print(
|
|
f"\n\033[95m\033[1mAdvent of Code {year}\033[0m"
|
|
"\n###################"
|
|
"\n\n\033[96mby Anders Englöf Ytterström\033[0m"
|
|
)
|
|
|
|
|
|
stars = 0
|
|
for i in [str(n).zfill(2) for n in range(1, 26)]:
|
|
if not day_no or day_no.zfill(2) == i:
|
|
try:
|
|
day = __import__(
|
|
"output.day_{}".format(i),
|
|
globals(),
|
|
locals(),
|
|
["solve"],
|
|
0,
|
|
)
|
|
with open(f"./input/{i}.txt", "r") as f:
|
|
data = f.read().strip()
|
|
headline(i)
|
|
try:
|
|
data = day.presolve(data)
|
|
except AttributeError:
|
|
pass
|
|
try:
|
|
p1, p2 = day.solve(data)
|
|
except AttributeError:
|
|
pass
|
|
if p1:
|
|
print(f" \033[92m1)\033[0m {p1}")
|
|
stars += 1
|
|
if p2:
|
|
print(f" \033[92m2)\033[0m {p2}")
|
|
stars += 1
|
|
except IOError:
|
|
pass
|
|
except ImportError:
|
|
pass
|
|
if not day_no:
|
|
print(f"\nStars: {stars}")
|
|
print("".join("*" if n < stars else "•" for n in range(50)))
|
|
print("")
|