Not gonna lie, This is not my gig skillwise. I had
much fun though!
A matrix would most likely make the code more
readable.
I relied massively on test cases here, so I share
them here instead of in the source code:
assert 1 == solve("""
X.....
.M....
..A...
...S..
..X...
""".strip())[0]
assert 2 == solve("""
XS....
.MA...
..AM..
...SX.
..X...
""".strip())[0]
assert 2 == solve("""
..S......
...A.....
....M....
.....X...
..X...M..
.......A.
........S
""".strip())[0]
assert 4 == solve("""
X.SS
M.AA
A.MM
S.XX
...M
...A
...S
""".strip())[0]
assert 4 == solve("""
...X...
...M...
.X.A...
XMASAMX
.A.....
.S.....
""".strip())[0]
assert 1 == solve("""
....X
...M.
..A..
.S...
""".strip())[0]
assert 2 == solve("""
...SX
..AM.
.MA..
XS...
""".strip())[0]
assert 2 == solve("""
......X
.....M.
....A..
...S...
..A....
.M.....
X......
""".strip())[0]
assert 1 == solve("""
M.S
.A.
M.S
""".strip())[1]
assert 1 == solve("""
M.M
.A.
S.S
""".strip())[1]
assert 1 == solve("""
S.M
.A.
S.M
""".strip())[1]
assert 1 == solve("""
S.S
.A.
M.M
""".strip())[1]
assert 1 == solve("""
S.S
.A.
M.M
""".strip())[1]
assert 1 == solve("""
.A.
M.M
.A.
S.S
""".strip())[1]
assert 1 == solve("""
M.M.
.A.A
S.S.
""".strip())[1]
assert 1 == solve("""
M.M
.A.
S.S
.A.
""".strip())[1]
assert 1 == solve("""
.M.M
A.A.
.S.S
""".strip())[1]
125 lines
3 KiB
Python
125 lines
3 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
|
|
|
|
print(
|
|
f"\nAdvent of Code {year}" "\n###################" "\n\nby Anders Englöf Ytterström"
|
|
)
|
|
|
|
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)
|
|
print(f"\n- creating output/day_{padded_no}.py")
|
|
with open("output/day_{}.py".format(padded_no), "w") as s:
|
|
s.write(
|
|
f"""
|
|
import re
|
|
from collections import deque, Counter
|
|
from heapq import heappop, heappush
|
|
from itertools import compress, combinations, chain
|
|
|
|
from output import matrix, D, DD, ADJ, ints, mhd, mdbg, vdbg
|
|
|
|
|
|
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"
|
|
)
|
|
print(
|
|
f"""
|
|
Done! start coding.
|
|
|
|
Puzzle link:
|
|
https://adventofcode.com/{year}/day/{day_no}
|
|
|
|
Puzzle input (copy and paste to input/{day_no.zfill(2)}.txt):
|
|
https://adventofcode.com/{year}/day/{day_no}/input
|
|
"""
|
|
)
|
|
exit(0)
|
|
|
|
|
|
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" 1. {p1}")
|
|
stars += 1
|
|
if p2:
|
|
print(f" 2. {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("")
|