Compare commits
58 commits
2019-rerun
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 81c4073f32 | |||
| 8b6d89fec5 | |||
| 4b1fa6bd65 | |||
| 5b754381b0 | |||
| fcbeb4bfdd | |||
| d7d5312786 | |||
| 3a9a7d4994 | |||
| 72574e753f | |||
| 1b7b30e296 | |||
| 2c2379788d | |||
| 096c3d3e5d | |||
| 0500ac41ab | |||
| 14520a4625 | |||
| ded5c4f28c | |||
| 9dc3f16766 | |||
| f5c3ee938a | |||
| 7688ddb763 | |||
| 7c2b4c835a | |||
| fbe6994e3a | |||
| 2af9dc566a | |||
| e6307795a4 | |||
| 9a7a9c878b | |||
| 1e807b5daf | |||
| 19aee1363a | |||
| 0eb961d31c | |||
| 0a812572db | |||
| 1b6502df7a | |||
| dc44be89bf | |||
| 81fe14a407 | |||
| 7b2b101b8b | |||
| 44f6aa0f53 | |||
| f751b3b8d5 | |||
| cb622409f9 | |||
| 5deb351504 | |||
| 81741232af | |||
| cb50d62e3f | |||
| d02f63a4ef | |||
| 8aa6363b9f | |||
| abaec4231f | |||
| 525c6aa96c | |||
| c33bdc3f02 | |||
| e75670eaf2 | |||
| 63da79b2a4 | |||
| 39e09dd36e | |||
| ae942ce803 | |||
| ffc5088813 | |||
| 3170846595 | |||
| 63a7ccd0e2 | |||
| cdd5f63be4 | |||
| 623253ac9d | |||
| 31bb5b7006 | |||
| c113042e17 | |||
| b1b5cda1e3 | |||
| 8d4af7e6e9 | |||
| c46c2c5126 | |||
| 2d51468eb1 | |||
| dd756cb9eb | |||
| 28af87c00b |
109 changed files with 3969 additions and 334 deletions
|
|
@ -46,7 +46,9 @@ if __name__ == "__main__":
|
||||||
|
|
||||||
solution.solve(dummy)
|
solution.solve(dummy)
|
||||||
# solution.solve_again(dummy)
|
# solution.solve_again(dummy)
|
||||||
""".strip().format(day=day_no, day_no=day_no.zfill(2), name=name)
|
""".strip().format(
|
||||||
|
day=day_no, day_no=day_no.zfill(2), name=name
|
||||||
|
)
|
||||||
+ "\n"
|
+ "\n"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -63,13 +65,18 @@ https://adventofcode.com/{year}/day/{day_no}/input
|
||||||
)
|
)
|
||||||
exit(0)
|
exit(0)
|
||||||
|
|
||||||
|
stars = 0
|
||||||
for i in [str(n).zfill(2) for n in range(1, 26)]:
|
for i in [str(n).zfill(2) for n in range(1, 26)]:
|
||||||
try:
|
try:
|
||||||
solution = __import__(
|
solution = __import__(
|
||||||
"solutions.day_{}".format(i), globals(), locals(), ["Solution"], 0
|
"solutions.day_{}".format(i), globals(), locals(), ["Solution"], 0
|
||||||
).Solution()
|
).Solution()
|
||||||
solution.show_results()
|
solution.show_results()
|
||||||
|
stars += 2
|
||||||
except IOError:
|
except IOError:
|
||||||
pass
|
pass
|
||||||
except ImportError:
|
except ImportError:
|
||||||
pass
|
pass
|
||||||
|
print(f"\nStars: {stars}")
|
||||||
|
print("".join("*" if n < stars else "•" for n in range(50)))
|
||||||
|
print("")
|
||||||
|
|
|
||||||
|
|
@ -14,9 +14,8 @@ class BaseSolution:
|
||||||
data = self.read_input(self.input_file)
|
data = self.read_input(self.input_file)
|
||||||
puzzle_input = self.parse_input(data)
|
puzzle_input = self.parse_input(data)
|
||||||
print(
|
print(
|
||||||
"\n\n{}\n{}\n\nPart 1: {}\nPart 2: {}".format(
|
"\n--- {} ---\n 1. {}\n 2. {}".format(
|
||||||
str(self),
|
str(self),
|
||||||
"-" * len(str(self)),
|
|
||||||
self.solve(puzzle_input),
|
self.solve(puzzle_input),
|
||||||
self.solve_again(puzzle_input),
|
self.solve_again(puzzle_input),
|
||||||
)
|
)
|
||||||
|
|
|
||||||
26
2015-python/solutions/day_01.py
Normal file
26
2015-python/solutions/day_01.py
Normal file
|
|
@ -0,0 +1,26 @@
|
||||||
|
from solutions import BaseSolution
|
||||||
|
|
||||||
|
|
||||||
|
class Solution(BaseSolution):
|
||||||
|
input_file = "01.txt"
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return "Day 1: Not Quite Lisp"
|
||||||
|
|
||||||
|
def solve(self, pi):
|
||||||
|
return sum([-1 if c == ")" else 1 for c in pi])
|
||||||
|
|
||||||
|
def solve_again(self, pi):
|
||||||
|
f = 1
|
||||||
|
for i, v in enumerate(pi):
|
||||||
|
f += -1 if v == ")" else 1
|
||||||
|
if f == -1:
|
||||||
|
return i
|
||||||
|
|
||||||
|
def parse_input(self, data):
|
||||||
|
return data.strip()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
solution = Solution()
|
||||||
|
solution.show_results()
|
||||||
33
2015-python/solutions/day_02.py
Normal file
33
2015-python/solutions/day_02.py
Normal file
|
|
@ -0,0 +1,33 @@
|
||||||
|
from solutions import BaseSolution
|
||||||
|
|
||||||
|
|
||||||
|
class Solution(BaseSolution):
|
||||||
|
input_file = "02.txt"
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return "Day 2: I Was Told There Would Be No Math"
|
||||||
|
|
||||||
|
def solve(self, pi):
|
||||||
|
o = self._solve(pi)
|
||||||
|
return o[0]
|
||||||
|
|
||||||
|
def solve_again(self, pi):
|
||||||
|
o = self._solve(pi)
|
||||||
|
return o[1]
|
||||||
|
|
||||||
|
def _solve(self, pi):
|
||||||
|
p1 = 0
|
||||||
|
p2 = 0
|
||||||
|
for p in pi.splitlines():
|
||||||
|
l, w, h = sorted([int(s) for s in p.split("x")])
|
||||||
|
p1 += 2 * (l * w + w * h + h * l) + min([l * w, w * h, h * l])
|
||||||
|
p2 += 2 * l + 2 * w + l * w * h
|
||||||
|
return p1, p2
|
||||||
|
|
||||||
|
def parse_input(self, data):
|
||||||
|
return data.strip()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
solution = Solution()
|
||||||
|
solution.show_results()
|
||||||
47
2015-python/solutions/day_03.py
Normal file
47
2015-python/solutions/day_03.py
Normal file
|
|
@ -0,0 +1,47 @@
|
||||||
|
from collections import defaultdict
|
||||||
|
|
||||||
|
from solutions import BaseSolution
|
||||||
|
|
||||||
|
|
||||||
|
class Solution(BaseSolution):
|
||||||
|
input_file = "03.txt"
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return "Day 3: Perfectly Spherical Houses in a Vacuum"
|
||||||
|
|
||||||
|
def solve(self, pi):
|
||||||
|
return self._solve(pi)[0]
|
||||||
|
|
||||||
|
def solve_again(self, pi):
|
||||||
|
return self._solve(pi)[1]
|
||||||
|
|
||||||
|
def _solve(self, pi):
|
||||||
|
def f(q):
|
||||||
|
p = (0, 0)
|
||||||
|
vs = defaultdict(int)
|
||||||
|
vs[p] += 1
|
||||||
|
for d in q:
|
||||||
|
r, c = p
|
||||||
|
match d:
|
||||||
|
case "^":
|
||||||
|
p = (r - 1, c)
|
||||||
|
case ">":
|
||||||
|
p = (r, c + 1)
|
||||||
|
case "v":
|
||||||
|
p = (r + 1, c)
|
||||||
|
case "<":
|
||||||
|
p = (r, c - 1)
|
||||||
|
vs[p] += 1
|
||||||
|
return set(vs.keys())
|
||||||
|
|
||||||
|
p1 = len(f(pi))
|
||||||
|
p2 = len(f(pi[0::2]) | f(pi[1::2]))
|
||||||
|
return p1, p2
|
||||||
|
|
||||||
|
def parse_input(self, data):
|
||||||
|
return data.strip()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
solution = Solution()
|
||||||
|
solution.show_results()
|
||||||
34
2015-python/solutions/day_04.py
Normal file
34
2015-python/solutions/day_04.py
Normal file
|
|
@ -0,0 +1,34 @@
|
||||||
|
from hashlib import md5
|
||||||
|
|
||||||
|
from solutions import BaseSolution
|
||||||
|
|
||||||
|
|
||||||
|
class Solution(BaseSolution):
|
||||||
|
input_file = "04.txt"
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return "Day 4: The Ideal Stocking Stuffer"
|
||||||
|
|
||||||
|
def solve(self, pi):
|
||||||
|
return self._solve(pi)[0]
|
||||||
|
|
||||||
|
def solve_again(self, pi):
|
||||||
|
return self._solve(pi)[1]
|
||||||
|
|
||||||
|
def _solve(self, secret):
|
||||||
|
p12 = []
|
||||||
|
prefetched = [254575, 1038736]
|
||||||
|
for zc in [5, 6]:
|
||||||
|
sw = str.zfill("0", zc)
|
||||||
|
c = prefetched.pop(0)
|
||||||
|
if md5(bytes(f"{secret}{c}", "utf-8")).hexdigest().startswith(sw):
|
||||||
|
p12.append(c)
|
||||||
|
return p12
|
||||||
|
|
||||||
|
def parse_input(self, data):
|
||||||
|
return data.strip()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
solution = Solution()
|
||||||
|
solution.show_results()
|
||||||
39
2015-python/solutions/day_05.py
Normal file
39
2015-python/solutions/day_05.py
Normal file
|
|
@ -0,0 +1,39 @@
|
||||||
|
import re
|
||||||
|
|
||||||
|
from solutions import BaseSolution
|
||||||
|
|
||||||
|
|
||||||
|
class Solution(BaseSolution):
|
||||||
|
input_file = "05.txt"
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return "Day 5: Doesn't He Have Intern-Elves For This?"
|
||||||
|
|
||||||
|
def solve(self, pi):
|
||||||
|
return self._solve(pi)[0]
|
||||||
|
|
||||||
|
def solve_again(self, pi):
|
||||||
|
return self._solve(pi)[1]
|
||||||
|
|
||||||
|
def parse_input(self, data):
|
||||||
|
return data.strip()
|
||||||
|
|
||||||
|
def _solve(self, pi):
|
||||||
|
wl = pi.split()
|
||||||
|
p1 = sum(
|
||||||
|
not re.search(r"ab|cd|pq|xy.*", w)
|
||||||
|
and any(w[i] == w[i + 1] for i in range(len(w) - 1))
|
||||||
|
and len(re.findall(r"[aeiou]", w)) > 2
|
||||||
|
for w in wl
|
||||||
|
)
|
||||||
|
p2 = sum(
|
||||||
|
any(w.count(w[i : i + 2]) == 2 for i in range(len(w) - 1))
|
||||||
|
and any(w[i] == w[i + 2] for i in range(len(w) - 2))
|
||||||
|
for w in wl
|
||||||
|
)
|
||||||
|
return p1, p2
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
solution = Solution()
|
||||||
|
solution.show_results()
|
||||||
66
2015-python/solutions/day_07.py
Normal file
66
2015-python/solutions/day_07.py
Normal file
|
|
@ -0,0 +1,66 @@
|
||||||
|
from collections import defaultdict
|
||||||
|
|
||||||
|
from solutions import BaseSolution
|
||||||
|
|
||||||
|
|
||||||
|
class Solution(BaseSolution):
|
||||||
|
input_file = "07.txt"
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return "Day 7: Some Assembly Required"
|
||||||
|
|
||||||
|
def solve(self, pi):
|
||||||
|
return self._solve(pi)
|
||||||
|
|
||||||
|
def solve_again(self, pi):
|
||||||
|
a = self.solve(pi)
|
||||||
|
return self._solve(pi.replace("19138", str(a)))
|
||||||
|
|
||||||
|
def parse_input(self, data):
|
||||||
|
return data.strip()
|
||||||
|
|
||||||
|
def _solve(self, pi):
|
||||||
|
p = pi.splitlines()
|
||||||
|
w = defaultdict(int)
|
||||||
|
|
||||||
|
while p:
|
||||||
|
np = []
|
||||||
|
for l in p:
|
||||||
|
x, to = l.split(" -> ")
|
||||||
|
if x.isdigit():
|
||||||
|
w[to] += int(x)
|
||||||
|
elif len(x.split()) == 1:
|
||||||
|
if x not in w:
|
||||||
|
np.append(l)
|
||||||
|
else:
|
||||||
|
w[to] += w[x]
|
||||||
|
elif x.startswith("NOT "):
|
||||||
|
a = x.split()[-1]
|
||||||
|
if a.isdigit() or a in w:
|
||||||
|
a = int(a) if a.isdigit() else w[a]
|
||||||
|
w[to] += ~a
|
||||||
|
else:
|
||||||
|
np.append(l)
|
||||||
|
else:
|
||||||
|
a, v, b = x.split()
|
||||||
|
if (a.isdigit() or a in w) and (b.isdigit() or b in w):
|
||||||
|
a = int(a) if a.isdigit() else w[a]
|
||||||
|
b = int(b) if b.isdigit() else w[b]
|
||||||
|
match v:
|
||||||
|
case "RSHIFT":
|
||||||
|
w[to] += a >> b
|
||||||
|
case "LSHIFT":
|
||||||
|
w[to] += a << b
|
||||||
|
case "AND":
|
||||||
|
w[to] += a & b
|
||||||
|
case "OR":
|
||||||
|
w[to] += a | b
|
||||||
|
else:
|
||||||
|
np.append(l)
|
||||||
|
p = np
|
||||||
|
return w["a"]
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
solution = Solution()
|
||||||
|
solution.show_results()
|
||||||
50
2016-python2/README.md
Normal file
50
2016-python2/README.md
Normal file
|
|
@ -0,0 +1,50 @@
|
||||||
|
# Advent of Code 2016
|
||||||
|
|
||||||
|
Solutions for #aoc2016 in Python 3 (3.12.7).
|
||||||
|
|
||||||
|
## Setup
|
||||||
|
|
||||||
|
Since I want to remember, this is what was used to solve
|
||||||
|
the puzzles.
|
||||||
|
|
||||||
|
- Lenovo Thinkpad x260 laptop with Arch Linux.
|
||||||
|
- Hyprland with gBar.
|
||||||
|
- Editor: Zed.
|
||||||
|
- Terminal: Alacritty.
|
||||||
|
|
||||||
|
## Help scripts
|
||||||
|
|
||||||
|
Display all solved puzzles:
|
||||||
|
|
||||||
|
python aoc.py
|
||||||
|
|
||||||
|
To bootstrap a new puzzle (creates `input/<day_no>.txt` and `output/day_<day_no>.py`):
|
||||||
|
|
||||||
|
python aoc.py <day_no> <puzzle_name>
|
||||||
|
|
||||||
|
Manually copy the puzzle input from https://adventofcode.com and paste it in `input/<day_no>.txt`
|
||||||
|
to start coding.
|
||||||
|
|
||||||
|
wl-paste > input/<day_no>.txt
|
||||||
|
|
||||||
|
Solve separate puzzle (replace `XX` with the puzzle number):
|
||||||
|
|
||||||
|
python -m output.day_XX
|
||||||
|
|
||||||
|
Solve separate puzzle using stdin (replace `XX` with the puzzle number):
|
||||||
|
|
||||||
|
wl-paste | python -m output.day_XX
|
||||||
|
cat tmpfile | python -m output.day_XX
|
||||||
|
|
||||||
|
Execute separate puzzle on file save (replace `XX` with the puzzle number):
|
||||||
|
|
||||||
|
ls output/*.py | entr -c -s 'wlpaste | python -m output.day_XX'
|
||||||
|
ls output/*.py | entr -c -s 'cat tmpfile | python -m output.day_XX'
|
||||||
|
ls output/*.py | entr -c -r python -m output.day_XX
|
||||||
|
|
||||||
|
(requires `entr` and `wl-paste`, Mac users can instead use `pbpaste`. If you
|
||||||
|
prefer X at Linux, use `xclip -selection clipboard -o`).
|
||||||
|
|
||||||
|
To lint files:
|
||||||
|
|
||||||
|
ls output/*.py | entr -r -c flake8 output --ignore=E741,E501,E203
|
||||||
121
2016-python2/aoc.py
Normal file
121
2016-python2/aoc.py
Normal file
|
|
@ -0,0 +1,121 @@
|
||||||
|
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"\n--- Day {n} ---\n")
|
||||||
|
|
||||||
|
|
||||||
|
year = 2016
|
||||||
|
|
||||||
|
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, defaultdict
|
||||||
|
from heapq import heappop, heappush
|
||||||
|
from itertools import compress, combinations, chain, permutations
|
||||||
|
|
||||||
|
from output import matrix, D, DD, ADJ, ints, mhd, mdbg, vdbg
|
||||||
|
|
||||||
|
|
||||||
|
def solve(data):
|
||||||
|
p1 = 1
|
||||||
|
p2 = 2
|
||||||
|
return p1, p2
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
# 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)
|
||||||
|
# print(p2)
|
||||||
|
|
||||||
|
# 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("")
|
||||||
76
2016-python2/output/__init__.py
Normal file
76
2016-python2/output/__init__.py
Normal file
|
|
@ -0,0 +1,76 @@
|
||||||
|
import functools
|
||||||
|
import re
|
||||||
|
|
||||||
|
# Directions/Adjacents for 2D matrices, in the order UP, RIGHT, DOWN, LEFT
|
||||||
|
D = [
|
||||||
|
(-1, 0),
|
||||||
|
(0, 1),
|
||||||
|
(1, 0),
|
||||||
|
(0, -1),
|
||||||
|
]
|
||||||
|
|
||||||
|
# Directions for 2D matrices, as a dict with keys U, R, D, L
|
||||||
|
DD = {
|
||||||
|
"U": (-1, 0),
|
||||||
|
"R": (0, 1),
|
||||||
|
"D": (1, 0),
|
||||||
|
"L": (0, -1),
|
||||||
|
}
|
||||||
|
|
||||||
|
# Adjacent relative positions including diagonals for 2D matrices, in the order NW, N, NE, W, E, SW, S, SE
|
||||||
|
ADJ = [
|
||||||
|
(-1, -1),
|
||||||
|
(-1, 0),
|
||||||
|
(1, -1),
|
||||||
|
(0, -1),
|
||||||
|
(0, 1),
|
||||||
|
(1, 1),
|
||||||
|
(1, 0),
|
||||||
|
(1, -1),
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def ints(s):
|
||||||
|
"""Extract all integers from a string"""
|
||||||
|
return [int(n) for n in re.findall(r"\d+", s)]
|
||||||
|
|
||||||
|
|
||||||
|
def mhd(a, b):
|
||||||
|
"""Calculates the Manhattan distance between 2 positions in the format (y, x) or (x, y)"""
|
||||||
|
ar, ac = a
|
||||||
|
br, bc = b
|
||||||
|
return abs(ar - br) + abs(ac - bc)
|
||||||
|
|
||||||
|
|
||||||
|
def matrix(d):
|
||||||
|
"""Transform a string into an iterable matrix. Returns the matrix, row count and col count"""
|
||||||
|
m = [tuple(r) for r in d.split()]
|
||||||
|
return m, len(m), len(m[0])
|
||||||
|
|
||||||
|
|
||||||
|
def mdbg(m):
|
||||||
|
"""Print-debug a matrix"""
|
||||||
|
for r in m:
|
||||||
|
print("".join(r))
|
||||||
|
|
||||||
|
|
||||||
|
def vdbg(seen, h, w):
|
||||||
|
"""Print-debug visited positions of a matrix"""
|
||||||
|
for r in range(h):
|
||||||
|
print("".join(["#" if (r, c) in seen else "." for c in range(w)]))
|
||||||
|
|
||||||
|
|
||||||
|
def bfs(S, E=None):
|
||||||
|
"""BFS algorithm, equal weighted nodes"""
|
||||||
|
seen = set()
|
||||||
|
q = [(S, 0)]
|
||||||
|
g = {} # graph, required to be provided at some point
|
||||||
|
while q:
|
||||||
|
m, w = q.pop(0)
|
||||||
|
if m in seen:
|
||||||
|
continue
|
||||||
|
seen.add(m)
|
||||||
|
# investigate here
|
||||||
|
for s in g[m]:
|
||||||
|
q.append((s, w + 1))
|
||||||
|
# return insights
|
||||||
136
2016-python2/output/day_11.py
Normal file
136
2016-python2/output/day_11.py
Normal file
|
|
@ -0,0 +1,136 @@
|
||||||
|
import re
|
||||||
|
from collections import Counter, deque
|
||||||
|
from itertools import combinations
|
||||||
|
|
||||||
|
D = {1: [2], 2: [1, 3], 3: [2, 4], 4: [3]}
|
||||||
|
|
||||||
|
|
||||||
|
def solve(data):
|
||||||
|
def parse(row):
|
||||||
|
return sorted(
|
||||||
|
[
|
||||||
|
"".join(nt).upper()
|
||||||
|
for nt in re.findall(r"(\w)\S+(?:-compatible)? (g|m)", row)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
f = dict([(i, parse(row)) for i, row in enumerate(data.splitlines(), start=1)])
|
||||||
|
E = sum(map(len, f.values()))
|
||||||
|
p1 = bfs(1, f, E)
|
||||||
|
|
||||||
|
adjusted = data.splitlines()
|
||||||
|
adjusted[
|
||||||
|
0
|
||||||
|
] += """
|
||||||
|
An elerium generator.
|
||||||
|
An elerium-compatible microchip.
|
||||||
|
A dilithium generator.
|
||||||
|
A dilithium-compatible microchip.
|
||||||
|
"""
|
||||||
|
f = dict([(i, parse(row)) for i, row in enumerate(adjusted, start=1)])
|
||||||
|
E = sum(map(len, f.values()))
|
||||||
|
p2 = bfs(1, f, E)
|
||||||
|
return p1, p2
|
||||||
|
|
||||||
|
|
||||||
|
def bfs(S, m, E):
|
||||||
|
seen = set()
|
||||||
|
q = deque([(S, m, 0)])
|
||||||
|
while q:
|
||||||
|
e, m, w = q.popleft()
|
||||||
|
cs = seen_checksum(e, m) # reddit wisdom, see function for more info
|
||||||
|
if cs in seen:
|
||||||
|
continue
|
||||||
|
seen.add(cs)
|
||||||
|
|
||||||
|
if len(m[4]) == E:
|
||||||
|
return w
|
||||||
|
|
||||||
|
for n, b, a in valid_next_moves(e, m):
|
||||||
|
ns = m.copy()
|
||||||
|
ns[e] = a
|
||||||
|
ns[n] = b
|
||||||
|
q.append((n, ns, w + 1))
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def valid_next_moves(e, S):
|
||||||
|
g = []
|
||||||
|
for n in D[e]:
|
||||||
|
for o in [[x] for x in S[e]] + [list(x) for x in combinations(S[e], 2)]:
|
||||||
|
a = sorted(x for x in S[e] if x not in o)
|
||||||
|
b = sorted(S[n] + o)
|
||||||
|
if is_valid(a) and is_valid(b):
|
||||||
|
g.append((n, b, a))
|
||||||
|
return g
|
||||||
|
|
||||||
|
|
||||||
|
def is_valid(f):
|
||||||
|
g = [x for x in f if x.endswith("G")]
|
||||||
|
if not g:
|
||||||
|
return True
|
||||||
|
mc = [x for x in f if x.endswith("M")]
|
||||||
|
return all(f"{m[0]}G" in f for m in mc)
|
||||||
|
|
||||||
|
|
||||||
|
def seen_checksum(e, s):
|
||||||
|
# To speed up execution, a handy trick was mentioned on several
|
||||||
|
# reddit threads.
|
||||||
|
#
|
||||||
|
# The vanilla BFS method is to store the complete state (elevator
|
||||||
|
# position + all floors), which this code like many others did initially.
|
||||||
|
# This is fine, but will lead to really long execution times.
|
||||||
|
#
|
||||||
|
# The common wisdom boils down to one thing: it does not matter _what_
|
||||||
|
# name the microchips and generators have. Only the arrangement (counts) of
|
||||||
|
# any microchips or generators across the floors do.
|
||||||
|
#
|
||||||
|
# For example, these are the same as a checksum to determine if a state has
|
||||||
|
# been seen:
|
||||||
|
#
|
||||||
|
# F2 . HG . . F2 . LG . .
|
||||||
|
# F1 E . HM LM F1 E . LM HM
|
||||||
|
#
|
||||||
|
# The H's or L's do not matter, only the M's and G's do.
|
||||||
|
#
|
||||||
|
# So by storing the M's and the G's as a checksum, along with the
|
||||||
|
# elevator position, the program is a lot faster.
|
||||||
|
#
|
||||||
|
# Reddit post, giving hints:
|
||||||
|
# https://www.reddit.com/r/adventofcode/comments/5hoia9/comment/db1v1ws/?utm_source=share&utm_medium=web3x&utm_name=web3xcss&utm_term=1&utm_content=share_button
|
||||||
|
# Blog post, providing code and reasoning:
|
||||||
|
# https://eddmann.com/posts/advent-of-code-2016-day-11-radioisotope-thermoelectric-generators/
|
||||||
|
|
||||||
|
# uncomment below line to get speed boost:
|
||||||
|
# %> 2.59s user 0.02s system 99% cpu 2.622 total
|
||||||
|
return e, tuple(tuple(Counter(x[-1] for x in f).most_common()) for f in s.values())
|
||||||
|
|
||||||
|
# uncomment below line to use vanilla BFS visited storage
|
||||||
|
# %> 896.11s user 2.58s system 99% cpu 15:01.35 total
|
||||||
|
# return e, tuple((f, tuple(mg)) for f, mg in s.items())
|
||||||
|
|
||||||
|
|
||||||
|
def M(s, e):
|
||||||
|
d = {}
|
||||||
|
for k, v in s.items():
|
||||||
|
for x in v:
|
||||||
|
d[x] = k - 1
|
||||||
|
l = len(d)
|
||||||
|
d = [
|
||||||
|
[v if x == k else ". " for x in range(l)]
|
||||||
|
for v, k in sorted(d.items(), key=lambda kv: kv[0])
|
||||||
|
]
|
||||||
|
m = [("F1", "F2", "F3", "F4"), ["E " if x == e - 1 else ". " for x in range(l)]] + d
|
||||||
|
for r in list(zip(*m))[::-1]:
|
||||||
|
print(" ".join(r))
|
||||||
|
print("")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
with open("./input/11.txt", "r") as f:
|
||||||
|
inp = f.read().strip()
|
||||||
|
|
||||||
|
p1, p2 = solve(inp)
|
||||||
|
|
||||||
|
print(p1)
|
||||||
|
print(p2)
|
||||||
52
2016-python2/output/day_12.py
Normal file
52
2016-python2/output/day_12.py
Normal file
|
|
@ -0,0 +1,52 @@
|
||||||
|
def solve(data):
|
||||||
|
p = data.splitlines()
|
||||||
|
pl = len(p)
|
||||||
|
|
||||||
|
def run(r):
|
||||||
|
i = 0
|
||||||
|
while i < pl:
|
||||||
|
o, *a = p[i].split()
|
||||||
|
match o:
|
||||||
|
case "cpy":
|
||||||
|
kv, t = a
|
||||||
|
r[t] = int(kv) if kv.isdigit() else r[kv]
|
||||||
|
i += 1
|
||||||
|
case "jnz":
|
||||||
|
kv, d = a
|
||||||
|
c = int(kv) if kv.isdigit() else r[kv]
|
||||||
|
i += 1 if c == 0 else int(d)
|
||||||
|
case "inc":
|
||||||
|
r[a[0]] += 1
|
||||||
|
i += 1
|
||||||
|
case "dec":
|
||||||
|
r[a[0]] -= 1
|
||||||
|
i += 1
|
||||||
|
return r["a"]
|
||||||
|
|
||||||
|
p1 = run(
|
||||||
|
{
|
||||||
|
"a": 0,
|
||||||
|
"b": 0,
|
||||||
|
"c": 0,
|
||||||
|
"d": 0,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
p2 = run(
|
||||||
|
{
|
||||||
|
"a": 0,
|
||||||
|
"b": 0,
|
||||||
|
"c": 1,
|
||||||
|
"d": 0,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
return p1, p2
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
with open("./input/12.txt", "r") as f:
|
||||||
|
inp = f.read().strip()
|
||||||
|
|
||||||
|
p1, p2 = solve(inp)
|
||||||
|
|
||||||
|
print(p1)
|
||||||
|
print(p2)
|
||||||
52
2016-python2/output/day_13.py
Normal file
52
2016-python2/output/day_13.py
Normal file
|
|
@ -0,0 +1,52 @@
|
||||||
|
from collections import Counter
|
||||||
|
from math import inf
|
||||||
|
|
||||||
|
from output import D
|
||||||
|
|
||||||
|
|
||||||
|
def solve(data, E=(31, 39)):
|
||||||
|
S = (1, 1)
|
||||||
|
p1, p2 = bfs(S, E, int(data), inf, 0)
|
||||||
|
return p1, p2
|
||||||
|
|
||||||
|
|
||||||
|
def bfs(S, E, z, p1, p2):
|
||||||
|
seen = set()
|
||||||
|
q = [(S, 0)]
|
||||||
|
while q:
|
||||||
|
m, w = q.pop(0)
|
||||||
|
if m in seen:
|
||||||
|
continue
|
||||||
|
seen.add(m)
|
||||||
|
|
||||||
|
if m == E:
|
||||||
|
p1 = min(w, p1)
|
||||||
|
if w <= 50:
|
||||||
|
p2 = max(p2, len(seen))
|
||||||
|
|
||||||
|
x, y = m
|
||||||
|
g = [
|
||||||
|
(x, y)
|
||||||
|
for x, y in [(x + c, y + r) for r, c in D]
|
||||||
|
if x >= 0 and y >= 0 and valid(x, y, z)
|
||||||
|
]
|
||||||
|
|
||||||
|
for s in g:
|
||||||
|
q.append((s, w + 1))
|
||||||
|
return p1, p2
|
||||||
|
|
||||||
|
|
||||||
|
def valid(x, y, z):
|
||||||
|
n = (x * x + 3 * x + 2 * x * y + y + y * y) + z
|
||||||
|
s = Counter(f"{n:b}")["1"]
|
||||||
|
return s % 2 == 0
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
with open("./input/13.txt", "r") as f:
|
||||||
|
inp = f.read().strip()
|
||||||
|
|
||||||
|
p1, p2 = solve(inp)
|
||||||
|
|
||||||
|
print(p1)
|
||||||
|
print(p2)
|
||||||
57
2016-python2/output/day_14.py
Normal file
57
2016-python2/output/day_14.py
Normal file
|
|
@ -0,0 +1,57 @@
|
||||||
|
import functools
|
||||||
|
import re
|
||||||
|
from hashlib import md5
|
||||||
|
|
||||||
|
|
||||||
|
def solve(s):
|
||||||
|
p1 = run(s)
|
||||||
|
p2 = run(s, p2=True)
|
||||||
|
return p1, p2
|
||||||
|
|
||||||
|
|
||||||
|
def run(s, p2=False):
|
||||||
|
r3 = re.compile(r"(\w)\1{2}")
|
||||||
|
c = 0
|
||||||
|
i = 0
|
||||||
|
H = md5(s.encode())
|
||||||
|
while True:
|
||||||
|
i += 1
|
||||||
|
if p2:
|
||||||
|
a = stretch(f"{s}{i}")
|
||||||
|
else:
|
||||||
|
Ha = H.copy()
|
||||||
|
Ha.update(str(i).encode())
|
||||||
|
a = Ha.hexdigest()
|
||||||
|
if m := re.search(r3, a):
|
||||||
|
x = m.group(1)
|
||||||
|
r5 = re.compile(r"(" + x + r")\1{4}")
|
||||||
|
for j in range(1000):
|
||||||
|
if p2:
|
||||||
|
b = stretch(f"{s}{str(i + 1 + j)}")
|
||||||
|
else:
|
||||||
|
Hb = H.copy()
|
||||||
|
Hb.update(str(i + 1 + j).encode())
|
||||||
|
b = Hb.hexdigest()
|
||||||
|
if re.search(r5, b):
|
||||||
|
c += 1
|
||||||
|
break
|
||||||
|
if c == 64:
|
||||||
|
break
|
||||||
|
return i
|
||||||
|
|
||||||
|
|
||||||
|
@functools.lru_cache(maxsize=None)
|
||||||
|
def stretch(s):
|
||||||
|
for _ in range(2017):
|
||||||
|
s = md5(s.encode()).hexdigest()
|
||||||
|
return s
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
with open("./input/14.txt", "r") as f:
|
||||||
|
inp = f.read().strip()
|
||||||
|
|
||||||
|
p1, p2 = solve(inp)
|
||||||
|
|
||||||
|
print(p1)
|
||||||
|
print(p2)
|
||||||
30
2016-python2/output/day_15.py
Normal file
30
2016-python2/output/day_15.py
Normal file
|
|
@ -0,0 +1,30 @@
|
||||||
|
import re
|
||||||
|
|
||||||
|
|
||||||
|
def solve(data):
|
||||||
|
M = [
|
||||||
|
tuple(map(int, re.search(r"(\d+) pos.+ion (\d+)", r).groups()))
|
||||||
|
for r in data.splitlines()
|
||||||
|
]
|
||||||
|
p1 = wait_and_press(M)
|
||||||
|
p2 = wait_and_press(M + [(11, 0)])
|
||||||
|
return p1, p2
|
||||||
|
|
||||||
|
|
||||||
|
def wait_and_press(M):
|
||||||
|
t = 0
|
||||||
|
while True:
|
||||||
|
if all((ti + lp[1]) % lp[0] == 0 for ti, lp in enumerate(M, start=t + 1)):
|
||||||
|
break
|
||||||
|
t += 1
|
||||||
|
return t
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
with open("./input/15.txt", "r") as f:
|
||||||
|
inp = f.read().strip()
|
||||||
|
|
||||||
|
p1, p2 = solve(inp)
|
||||||
|
|
||||||
|
print(p1)
|
||||||
|
print(p2)
|
||||||
24
2016-python2/output/day_16.py
Normal file
24
2016-python2/output/day_16.py
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
def solve(data):
|
||||||
|
p12 = []
|
||||||
|
for DS in [272, 35651584]:
|
||||||
|
s = [int(c) for c in data]
|
||||||
|
while len(s) < DS:
|
||||||
|
b = [abs(int(c) - 1) for c in s[::-1]]
|
||||||
|
s = s + [0] + b
|
||||||
|
s = s[:DS]
|
||||||
|
p = len(s)
|
||||||
|
while p % 2 == 0:
|
||||||
|
s = [int(a == b) for a, b in zip(s[::2], s[1::2])]
|
||||||
|
p = len(s)
|
||||||
|
p12.append("".join(map(str, s)))
|
||||||
|
return p12
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
with open("./input/16.txt", "r") as f:
|
||||||
|
inp = f.read().strip()
|
||||||
|
|
||||||
|
p1, p2 = solve(inp)
|
||||||
|
|
||||||
|
print(p1)
|
||||||
|
print(p2)
|
||||||
41
2016-python2/output/day_17.py
Normal file
41
2016-python2/output/day_17.py
Normal file
|
|
@ -0,0 +1,41 @@
|
||||||
|
from collections import deque
|
||||||
|
from hashlib import md5
|
||||||
|
|
||||||
|
from output import DD
|
||||||
|
|
||||||
|
|
||||||
|
def solve(data):
|
||||||
|
paths = list(bfs(data))
|
||||||
|
p1 = "".join(paths[0])
|
||||||
|
p2 = len(paths[-1])
|
||||||
|
return p1, p2
|
||||||
|
|
||||||
|
|
||||||
|
def bfs(code):
|
||||||
|
q = deque([((0, 0), [])])
|
||||||
|
T = (3, 3)
|
||||||
|
K = "UDLR"
|
||||||
|
while q:
|
||||||
|
m, d = q.popleft()
|
||||||
|
U, D, L, R, *_ = md5((code + "".join(d)).encode()).hexdigest()
|
||||||
|
y, x = m
|
||||||
|
for udlr in [K[k] for k, n in enumerate([U, D, L, R]) if isopen(n)]:
|
||||||
|
dy, dx = DD[udlr]
|
||||||
|
if (y + dy, x + dx) == T:
|
||||||
|
yield d + [udlr]
|
||||||
|
elif 0 <= y + dy < 4 and 0 <= x + dx < 4:
|
||||||
|
q.append(((y + dy, x + dx), d + [udlr]))
|
||||||
|
|
||||||
|
|
||||||
|
def isopen(c):
|
||||||
|
return c in "bcdef"
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
with open("./input/17.txt", "r") as f:
|
||||||
|
inp = f.read().strip()
|
||||||
|
|
||||||
|
p1, p2 = solve(inp)
|
||||||
|
|
||||||
|
print(p1)
|
||||||
|
print(p2)
|
||||||
32
2016-python2/output/day_18.py
Normal file
32
2016-python2/output/day_18.py
Normal file
|
|
@ -0,0 +1,32 @@
|
||||||
|
def solve(data):
|
||||||
|
cols = len(data)
|
||||||
|
prevrow = [t == "." for t in data]
|
||||||
|
p1 = sum(prevrow) + sum(
|
||||||
|
sum(prevrow := [issafe(prevrow, i) for i in range(cols)]) for _ in range(39)
|
||||||
|
)
|
||||||
|
p2 = p1 + sum(
|
||||||
|
sum(prevrow := [issafe(prevrow, i) for i in range(cols)])
|
||||||
|
for _ in range(400_000 - 40)
|
||||||
|
)
|
||||||
|
return p1, p2
|
||||||
|
|
||||||
|
|
||||||
|
def issafe(row, i):
|
||||||
|
match i:
|
||||||
|
case 0:
|
||||||
|
return row[1]
|
||||||
|
case n if n == len(row) - 1:
|
||||||
|
return row[-2]
|
||||||
|
case _:
|
||||||
|
lt, rt = row[i - 1], row[i + 1]
|
||||||
|
return not lt != rt
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
with open("./input/18.txt", "r") as f:
|
||||||
|
inp = f.read().strip()
|
||||||
|
|
||||||
|
p1, p2 = solve(inp)
|
||||||
|
|
||||||
|
print(p1)
|
||||||
|
print(p2)
|
||||||
52
2016-python2/output/day_19.py
Normal file
52
2016-python2/output/day_19.py
Normal file
|
|
@ -0,0 +1,52 @@
|
||||||
|
from collections import deque
|
||||||
|
|
||||||
|
|
||||||
|
def solve(data):
|
||||||
|
elfes = int(data)
|
||||||
|
p1 = left_adjacent_rule(elfes)
|
||||||
|
p1b = mathematical_superiority(elfes)
|
||||||
|
p2 = opposite_side_rule(elfes)
|
||||||
|
assert p1 == p1b
|
||||||
|
return p1, p2
|
||||||
|
|
||||||
|
|
||||||
|
def mathematical_superiority(num_elfes):
|
||||||
|
# Josephus' problem, quick method:
|
||||||
|
# https://www.youtube.com/watch?v=uCsD3ZGzMgE
|
||||||
|
b = format(num_elfes, "b")
|
||||||
|
return int(b[1:] + b[0], 2)
|
||||||
|
|
||||||
|
|
||||||
|
def left_adjacent_rule(num_elfes):
|
||||||
|
# https://en.wikipedia.org/wiki/Josephus_problem
|
||||||
|
q = deque(list(range(1, num_elfes + 1)))
|
||||||
|
while len(q) > 1:
|
||||||
|
q.rotate(-1)
|
||||||
|
q.popleft()
|
||||||
|
return q.pop()
|
||||||
|
|
||||||
|
|
||||||
|
def opposite_side_rule(num_elfes):
|
||||||
|
elfes = list(range(1, num_elfes + 1))
|
||||||
|
separator = num_elfes // 2
|
||||||
|
L, R = deque(elfes[:separator]), deque(elfes[separator:])
|
||||||
|
|
||||||
|
while L and R:
|
||||||
|
R.popleft()
|
||||||
|
l2r = L.popleft()
|
||||||
|
R.append(l2r)
|
||||||
|
if len(R) - len(L) != 1:
|
||||||
|
r2l = R.popleft()
|
||||||
|
L.append(r2l)
|
||||||
|
|
||||||
|
return R.pop()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
with open("./input/19.txt", "r") as f:
|
||||||
|
inp = f.read().strip()
|
||||||
|
|
||||||
|
p1, p2 = solve(inp)
|
||||||
|
|
||||||
|
print(p1)
|
||||||
|
print(p2)
|
||||||
31
2016-python2/output/day_20.py
Normal file
31
2016-python2/output/day_20.py
Normal file
|
|
@ -0,0 +1,31 @@
|
||||||
|
from output import ints
|
||||||
|
|
||||||
|
|
||||||
|
def solve(data):
|
||||||
|
lowest, highest = min(ints(data)), max(ints(data))
|
||||||
|
data = [ints(line) for line in data.split()]
|
||||||
|
p1 = float("inf")
|
||||||
|
p2 = set()
|
||||||
|
for a, b in data:
|
||||||
|
X = a - 1
|
||||||
|
Y = b + 1
|
||||||
|
if X >= lowest:
|
||||||
|
if not any(x <= X <= y for x, y in data):
|
||||||
|
p1 = min(p1, X)
|
||||||
|
p2.add(X)
|
||||||
|
if Y <= highest:
|
||||||
|
if not any(x <= Y <= y for x, y in data):
|
||||||
|
p1 = min(p1, Y)
|
||||||
|
p2.add(Y)
|
||||||
|
p2 = len(p2)
|
||||||
|
return p1, p2
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
with open("./input/20.txt", "r") as f:
|
||||||
|
inp = f.read().strip()
|
||||||
|
|
||||||
|
p1, p2 = solve(inp)
|
||||||
|
|
||||||
|
print(p1)
|
||||||
|
print(p2)
|
||||||
64
2016-python2/output/day_21.py
Normal file
64
2016-python2/output/day_21.py
Normal file
|
|
@ -0,0 +1,64 @@
|
||||||
|
import re
|
||||||
|
from itertools import permutations
|
||||||
|
|
||||||
|
from output import ints
|
||||||
|
|
||||||
|
|
||||||
|
def solve(data):
|
||||||
|
data = [line.strip() for line in data.splitlines()]
|
||||||
|
p1 = scramble(data, "abcdefgh")
|
||||||
|
p2 = unscramble(data, "fbgdceah")
|
||||||
|
return p1, p2
|
||||||
|
|
||||||
|
|
||||||
|
def scramble(data, subject):
|
||||||
|
r_swap = r"(?:letter|position) (.)"
|
||||||
|
for line in data:
|
||||||
|
if line.startswith("rotate right"):
|
||||||
|
x = ints(line)[0]
|
||||||
|
subject = subject[-x:] + subject[:-x]
|
||||||
|
if line.startswith("rotate left"):
|
||||||
|
x = ints(line)[0]
|
||||||
|
subject = subject[x:] + subject[:x]
|
||||||
|
if line.startswith("rotate based"):
|
||||||
|
x = re.findall(r"letter (.)", line)[0]
|
||||||
|
i = subject.index(x)
|
||||||
|
j = i + 1 % len(subject)
|
||||||
|
subject = subject[-j:] + subject[:-j]
|
||||||
|
if i >= 4:
|
||||||
|
subject = subject[-1:] + subject[:-1]
|
||||||
|
if line.startswith("swap letter"):
|
||||||
|
x, y = re.findall(r_swap, line)
|
||||||
|
subject = subject.replace(y, "#")
|
||||||
|
subject = subject.replace(x, y)
|
||||||
|
subject = subject.replace("#", x)
|
||||||
|
if line.startswith("swap position"):
|
||||||
|
x, y = ints(line)
|
||||||
|
v1, v2 = subject[x], subject[y]
|
||||||
|
subject = subject[:x] + v2 + subject[x + 1 :]
|
||||||
|
subject = subject[:y] + v1 + subject[y + 1 :]
|
||||||
|
if line.startswith("move"):
|
||||||
|
x, y = ints(line)
|
||||||
|
v = subject[x]
|
||||||
|
subject = subject[:x] + subject[x + 1 :]
|
||||||
|
subject = subject[:y] + v + subject[y:]
|
||||||
|
if line.startswith("reverse"):
|
||||||
|
x, y = ints(line)
|
||||||
|
subject = subject[:x] + subject[x : y + 1][::-1] + subject[y + 1 :]
|
||||||
|
return subject
|
||||||
|
|
||||||
|
|
||||||
|
def unscramble(data, T):
|
||||||
|
for candidate in ["".join(c) for c in permutations(T)]:
|
||||||
|
if scramble(data, candidate) == T:
|
||||||
|
return candidate
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
with open("./input/21.txt", "r") as f:
|
||||||
|
inp = f.read().strip()
|
||||||
|
|
||||||
|
p1, p2 = solve(inp)
|
||||||
|
|
||||||
|
print(p1)
|
||||||
|
print(p2)
|
||||||
45
2016-python2/output/day_22.py
Normal file
45
2016-python2/output/day_22.py
Normal file
|
|
@ -0,0 +1,45 @@
|
||||||
|
from itertools import permutations
|
||||||
|
|
||||||
|
from output import ints
|
||||||
|
|
||||||
|
|
||||||
|
def solve(data):
|
||||||
|
viable = set()
|
||||||
|
W = 0
|
||||||
|
H = 0
|
||||||
|
grid = dict()
|
||||||
|
for a, b in permutations([ints(line) for line in data.splitlines()[2:]], r=2):
|
||||||
|
x1, y1, size, used, _avail, _pc = a
|
||||||
|
x2, y2, _size, _used, avail, _pc = b
|
||||||
|
H = max([y1, y2, H])
|
||||||
|
W = max([x1, x2, W])
|
||||||
|
grid[(y1, x1)] = (used, size)
|
||||||
|
if 0 < used <= avail:
|
||||||
|
viable.add(((y1, x1), (y2, x2)))
|
||||||
|
if used == 0:
|
||||||
|
empty = (y1, x1)
|
||||||
|
p1 = len(viable)
|
||||||
|
S, E = (0, W), (0, 0)
|
||||||
|
# dagrid(grid, H + 1, W + 1)
|
||||||
|
y, x = empty
|
||||||
|
p2 = x + y + W + (W - 1) * 5
|
||||||
|
return p1, p2
|
||||||
|
|
||||||
|
|
||||||
|
def dagrid(grid, H, W):
|
||||||
|
"""Used to print the grid to be solved by hand."""
|
||||||
|
for r in range(H):
|
||||||
|
for c in range(W):
|
||||||
|
u, a = grid[(r, c)]
|
||||||
|
print(f"{u}/{a}".rjust(8), end="")
|
||||||
|
print("\n")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
with open("./input/22.txt", "r") as f:
|
||||||
|
inp = f.read().strip()
|
||||||
|
|
||||||
|
p1, p2 = solve(inp)
|
||||||
|
|
||||||
|
print(p1)
|
||||||
|
print(p2)
|
||||||
76
2016-python2/output/day_23.py
Normal file
76
2016-python2/output/day_23.py
Normal file
|
|
@ -0,0 +1,76 @@
|
||||||
|
def solve(data):
|
||||||
|
p = data.splitlines()
|
||||||
|
# math.factorial(7) + 5840
|
||||||
|
p1 = run(
|
||||||
|
p.copy(),
|
||||||
|
{
|
||||||
|
"a": 7,
|
||||||
|
"b": 0,
|
||||||
|
"c": 0,
|
||||||
|
"d": 0,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
# math.factorial(12) + 5840
|
||||||
|
p2 = run(
|
||||||
|
p.copy(),
|
||||||
|
{
|
||||||
|
"a": 12,
|
||||||
|
"b": 0,
|
||||||
|
"c": 0,
|
||||||
|
"d": 0,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
return p1, p2
|
||||||
|
|
||||||
|
|
||||||
|
def run(p, r):
|
||||||
|
i = 0
|
||||||
|
pl = len(p)
|
||||||
|
while i < pl:
|
||||||
|
o, *a = p[i].split()
|
||||||
|
match o:
|
||||||
|
case "cpy":
|
||||||
|
kv, t = a
|
||||||
|
if t in "abcd":
|
||||||
|
r[t] = int(kv) if kv not in "abcd" else r[kv]
|
||||||
|
i += 1
|
||||||
|
case "jnz":
|
||||||
|
kv, d = a
|
||||||
|
c = int(kv) if kv not in "abcd" else r[kv]
|
||||||
|
d = int(d) if d not in "abcd" else r[d]
|
||||||
|
i += 1 if c == 0 else d
|
||||||
|
case "inc":
|
||||||
|
r[a[0]] += 1
|
||||||
|
i += 1
|
||||||
|
case "dec":
|
||||||
|
r[a[0]] -= 1
|
||||||
|
i += 1
|
||||||
|
case "tgl":
|
||||||
|
kv = a[0]
|
||||||
|
c = int(kv) if kv not in "abcd" else r[kv]
|
||||||
|
if 0 <= i + c < pl:
|
||||||
|
old, *v = p[i + c].split()
|
||||||
|
match old:
|
||||||
|
case "inc":
|
||||||
|
nw = "dec"
|
||||||
|
case "dec":
|
||||||
|
nw = "inc"
|
||||||
|
case "tgl":
|
||||||
|
nw = "inc"
|
||||||
|
case "jnz":
|
||||||
|
nw = "cpy"
|
||||||
|
case _:
|
||||||
|
nw = "jnz"
|
||||||
|
p[i + c] = " ".join([nw] + v)
|
||||||
|
i += 1
|
||||||
|
return r["a"]
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
with open("./input/23.txt", "r") as f:
|
||||||
|
inp = f.read().strip()
|
||||||
|
|
||||||
|
p1, p2 = solve(inp)
|
||||||
|
|
||||||
|
print(p1)
|
||||||
|
print(p2)
|
||||||
58
2016-python2/output/day_24.py
Normal file
58
2016-python2/output/day_24.py
Normal file
|
|
@ -0,0 +1,58 @@
|
||||||
|
import re
|
||||||
|
from collections import Counter, defaultdict, deque
|
||||||
|
from heapq import heappop, heappush
|
||||||
|
from itertools import chain, combinations, compress, permutations
|
||||||
|
|
||||||
|
from output import ADJ, DD, D, ints, matrix, mdbg, mhd, vdbg
|
||||||
|
|
||||||
|
|
||||||
|
def solve(data):
|
||||||
|
grid, H, W = matrix(data)
|
||||||
|
dests = {
|
||||||
|
v: (y, x) for y, r in enumerate(grid) for x, v in enumerate(r) if v.isdigit()
|
||||||
|
}
|
||||||
|
S0 = dests["0"]
|
||||||
|
del dests["0"]
|
||||||
|
p1 = travel(dests, grid, H, W, S0)
|
||||||
|
p2 = travel(dests, grid, H, W, S0, goback=True)
|
||||||
|
return p1, p2
|
||||||
|
|
||||||
|
|
||||||
|
def travel(dests, grid, H, W, S0, goback=False):
|
||||||
|
shortest = float("inf")
|
||||||
|
for goals in permutations(dests.items()):
|
||||||
|
goals = list(goals)
|
||||||
|
if goback:
|
||||||
|
goals += [("0", S0)]
|
||||||
|
t = 0
|
||||||
|
S = S0
|
||||||
|
for _, E in goals:
|
||||||
|
seen = set()
|
||||||
|
q = [(S, 0)]
|
||||||
|
while q:
|
||||||
|
pos, w = q.pop(0)
|
||||||
|
if pos == E:
|
||||||
|
t += w
|
||||||
|
break
|
||||||
|
if pos in seen:
|
||||||
|
continue
|
||||||
|
seen.add(pos)
|
||||||
|
y, x = pos
|
||||||
|
for dy, dx in D:
|
||||||
|
if not (0 <= dy + y < H and 0 <= dx + x < W):
|
||||||
|
continue
|
||||||
|
if grid[dy + y][dx + x] != "#":
|
||||||
|
q.append(((dy + y, dx + x), w + 1))
|
||||||
|
S = E
|
||||||
|
shortest = min(shortest, t)
|
||||||
|
return shortest
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
with open("./input/24.txt", "r") as f:
|
||||||
|
inp = f.read().strip()
|
||||||
|
|
||||||
|
p1, p2 = solve(inp)
|
||||||
|
|
||||||
|
print(p1)
|
||||||
|
print(p2)
|
||||||
77
2016-python2/output/day_25.py
Normal file
77
2016-python2/output/day_25.py
Normal file
|
|
@ -0,0 +1,77 @@
|
||||||
|
def solve(data):
|
||||||
|
p = data.splitlines()
|
||||||
|
csl = 30
|
||||||
|
A = "01" * (csl // 2)
|
||||||
|
i = 1
|
||||||
|
while True:
|
||||||
|
if A == run(
|
||||||
|
csl,
|
||||||
|
p.copy(),
|
||||||
|
{
|
||||||
|
"a": i,
|
||||||
|
"b": 0,
|
||||||
|
"c": 0,
|
||||||
|
"d": 0,
|
||||||
|
},
|
||||||
|
):
|
||||||
|
break
|
||||||
|
i += 2
|
||||||
|
p1 = i
|
||||||
|
return p1, None
|
||||||
|
|
||||||
|
|
||||||
|
def run(lO, p, r):
|
||||||
|
i = 0
|
||||||
|
pl = len(p)
|
||||||
|
outs = []
|
||||||
|
while i < pl and len(outs) < lO:
|
||||||
|
o, *a = p[i].split()
|
||||||
|
match o:
|
||||||
|
case "cpy":
|
||||||
|
kv, t = a
|
||||||
|
if t in "abcd":
|
||||||
|
r[t] = int(kv) if kv not in "abcd" else r[kv]
|
||||||
|
i += 1
|
||||||
|
case "out":
|
||||||
|
kv, *_ = a
|
||||||
|
outs.append(f'{int(kv) if kv not in "abcd" else r[kv]}')
|
||||||
|
i += 1
|
||||||
|
case "jnz":
|
||||||
|
kv, d = a
|
||||||
|
c = int(kv) if kv not in "abcd" else r[kv]
|
||||||
|
d = int(d) if d not in "abcd" else r[d]
|
||||||
|
i += 1 if c == 0 else d
|
||||||
|
case "inc":
|
||||||
|
r[a[0]] += 1
|
||||||
|
i += 1
|
||||||
|
case "dec":
|
||||||
|
r[a[0]] -= 1
|
||||||
|
i += 1
|
||||||
|
case "tgl":
|
||||||
|
kv = a[0]
|
||||||
|
c = int(kv) if kv not in "abcd" else r[kv]
|
||||||
|
if 0 <= i + c < pl:
|
||||||
|
old, *v = p[i + c].split()
|
||||||
|
match old:
|
||||||
|
case "inc":
|
||||||
|
nw = "dec"
|
||||||
|
case "dec":
|
||||||
|
nw = "inc"
|
||||||
|
case "tgl":
|
||||||
|
nw = "inc"
|
||||||
|
case "jnz":
|
||||||
|
nw = "cpy"
|
||||||
|
case _:
|
||||||
|
nw = "jnz"
|
||||||
|
p[i + c] = " ".join([nw] + v)
|
||||||
|
i += 1
|
||||||
|
return "".join(outs)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
with open("./input/25.txt", "r") as f:
|
||||||
|
inp = f.read().strip()
|
||||||
|
|
||||||
|
p1, p2 = solve(inp)
|
||||||
|
|
||||||
|
print(p1)
|
||||||
|
|
@ -7,8 +7,9 @@ except ValueError:
|
||||||
name = None
|
name = None
|
||||||
|
|
||||||
if day_no and name:
|
if day_no and name:
|
||||||
with open('solutions/day_{}.py'.format(day_no.zfill(2)), 'w') as s:
|
with open("solutions/day_{}.py".format(day_no.zfill(2)), "w") as s:
|
||||||
s.write('''
|
s.write(
|
||||||
|
"""
|
||||||
from solutions import BaseSolution
|
from solutions import BaseSolution
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -28,9 +29,14 @@ class Solution(BaseSolution):
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
solution = Solution()
|
solution = Solution()
|
||||||
solution.show_results()
|
solution.show_results()
|
||||||
'''.strip().format(day=day_no, day_no=day_no.zfill(2), name=name) + '\n')
|
""".strip().format(
|
||||||
with open('tests/day_{}_tests.py'.format(day_no.zfill(2)), 'w') as t:
|
day=day_no, day_no=day_no.zfill(2), name=name
|
||||||
t.write('''
|
)
|
||||||
|
+ "\n"
|
||||||
|
)
|
||||||
|
with open("tests/day_{}_tests.py".format(day_no.zfill(2)), "w") as t:
|
||||||
|
t.write(
|
||||||
|
"""
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
from solutions.day_{day_no} import Solution
|
from solutions.day_{day_no} import Solution
|
||||||
|
|
@ -46,18 +52,24 @@ class Day{day_no}TestCase(unittest.TestCase):
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
'''.strip().format(day_no=day_no.zfill(2)) + '\n')
|
""".strip().format(
|
||||||
with open('inputs/{}.txt'.format(day_no.zfill(2)), 'w') as i:
|
day_no=day_no.zfill(2)
|
||||||
i.write('')
|
)
|
||||||
|
+ "\n"
|
||||||
|
)
|
||||||
|
with open("inputs/{}.txt".format(day_no.zfill(2)), "w") as i:
|
||||||
|
i.write("")
|
||||||
exit(0)
|
exit(0)
|
||||||
|
|
||||||
print('\nAdvent of Code 2017'
|
print(
|
||||||
'\n###################'
|
"\nAdvent of Code 2017" "\n###################" "\n\nby Anders Ytterström (@madr)"
|
||||||
'\n\nby Anders Ytterström (@madr)')
|
)
|
||||||
|
|
||||||
for i in [str(n).zfill(2) for n in range(1, 26)]:
|
for i in [str(n).zfill(2) for n in range(1, 26)]:
|
||||||
try:
|
try:
|
||||||
solution = __import__('solutions.day_{}'.format(i), globals(), locals(), ['Solution'], 0).Solution()
|
solution = __import__(
|
||||||
|
"solutions.day_{}".format(i), globals(), locals(), ["Solution"], 0
|
||||||
|
).Solution()
|
||||||
solution.show_results()
|
solution.show_results()
|
||||||
except IOError:
|
except IOError:
|
||||||
pass
|
pass
|
||||||
|
|
|
||||||
|
|
@ -4,20 +4,22 @@ class BaseSolution:
|
||||||
trim_input = True
|
trim_input = True
|
||||||
|
|
||||||
def parse_input(self, filename):
|
def parse_input(self, filename):
|
||||||
filepath = './inputs/{}'.format(filename)
|
filepath = "./inputs/{}".format(filename)
|
||||||
with open(filepath, 'r') as f:
|
with open(filepath, "r") as f:
|
||||||
self.puzzle_input = f.read()
|
self.puzzle_input = f.read()
|
||||||
if self.trim_input:
|
if self.trim_input:
|
||||||
self.puzzle_input = self.puzzle_input.strip()
|
self.puzzle_input = self.puzzle_input.strip()
|
||||||
|
|
||||||
def show_results(self):
|
def show_results(self):
|
||||||
self.parse_input(self.input_file)
|
self.parse_input(self.input_file)
|
||||||
print('\n\n{}\n{}\n\nPart 1: {}\nPart 2: {}'.format(
|
print(
|
||||||
|
"\n\n{}\n{}\n\nPart 1: {}\nPart 2: {}".format(
|
||||||
str(self),
|
str(self),
|
||||||
'-' * len(str(self)),
|
"-" * len(str(self)),
|
||||||
self.solve(self.puzzle_input),
|
self.solve(self.puzzle_input),
|
||||||
self.solve_again(self.puzzle_input),
|
self.solve_again(self.puzzle_input),
|
||||||
))
|
)
|
||||||
|
)
|
||||||
|
|
||||||
def solve(self, puzzle_input):
|
def solve(self, puzzle_input):
|
||||||
raise NotImplemented
|
raise NotImplemented
|
||||||
|
|
|
||||||
|
|
@ -2,21 +2,24 @@ from solutions import BaseSolution
|
||||||
|
|
||||||
|
|
||||||
class Solution(BaseSolution):
|
class Solution(BaseSolution):
|
||||||
input_file = '01.txt'
|
input_file = "01.txt"
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return 'Day 1: Inverse Captcha'
|
return "Day 1: Inverse Captcha"
|
||||||
|
|
||||||
def solve(self, puzzle_input, distance=1):
|
def solve(self, puzzle_input, distance=1):
|
||||||
pi_length = len(puzzle_input)
|
pi_length = len(puzzle_input)
|
||||||
return sum(int(puzzle_input[pos]) for pos in range(pi_length) if
|
return sum(
|
||||||
puzzle_input[pos] == puzzle_input[(pos + distance) % pi_length])
|
int(puzzle_input[pos])
|
||||||
|
for pos in range(pi_length)
|
||||||
|
if puzzle_input[pos] == puzzle_input[(pos + distance) % pi_length]
|
||||||
|
)
|
||||||
|
|
||||||
def solve_again(self, puzzle_input):
|
def solve_again(self, puzzle_input):
|
||||||
distance = len(puzzle_input) // 2
|
distance = len(puzzle_input) // 2
|
||||||
return self.solve(puzzle_input, distance)
|
return self.solve(puzzle_input, distance)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
solution = Solution()
|
solution = Solution()
|
||||||
solution.show_results()
|
solution.show_results()
|
||||||
|
|
|
||||||
|
|
@ -2,10 +2,10 @@ from solutions import BaseSolution
|
||||||
|
|
||||||
|
|
||||||
class Solution(BaseSolution):
|
class Solution(BaseSolution):
|
||||||
input_file = '02.txt'
|
input_file = "02.txt"
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return 'Day 2: Corruption Checksum'
|
return "Day 2: Corruption Checksum"
|
||||||
|
|
||||||
def _get_rows(self, puzzle_input):
|
def _get_rows(self, puzzle_input):
|
||||||
return [list(map(int, rows.split())) for rows in puzzle_input.splitlines()]
|
return [list(map(int, rows.split())) for rows in puzzle_input.splitlines()]
|
||||||
|
|
@ -29,6 +29,6 @@ class Solution(BaseSolution):
|
||||||
return sum(self.get_even_divisible(columns) for columns in rows)
|
return sum(self.get_even_divisible(columns) for columns in rows)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
solution = Solution()
|
solution = Solution()
|
||||||
solution.show_results()
|
solution.show_results()
|
||||||
|
|
|
||||||
|
|
@ -2,10 +2,10 @@ from solutions import BaseSolution
|
||||||
|
|
||||||
|
|
||||||
class Solution(BaseSolution):
|
class Solution(BaseSolution):
|
||||||
input_file = '03.txt'
|
input_file = "03.txt"
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return 'Day 3: Spiral Memory'
|
return "Day 3: Spiral Memory"
|
||||||
|
|
||||||
def _get_rounds(self, value):
|
def _get_rounds(self, value):
|
||||||
n = 0
|
n = 0
|
||||||
|
|
@ -32,6 +32,6 @@ class Solution(BaseSolution):
|
||||||
return 279138
|
return 279138
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
solution = Solution()
|
solution = Solution()
|
||||||
solution.show_results()
|
solution.show_results()
|
||||||
|
|
|
||||||
|
|
@ -2,15 +2,15 @@ from solutions import BaseSolution
|
||||||
|
|
||||||
|
|
||||||
class Solution(BaseSolution):
|
class Solution(BaseSolution):
|
||||||
input_file = '04.txt'
|
input_file = "04.txt"
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return 'Day 4: High-Entropy Passphrases'
|
return "Day 4: High-Entropy Passphrases"
|
||||||
|
|
||||||
def validate(self, passphrase, extended=False):
|
def validate(self, passphrase, extended=False):
|
||||||
words = passphrase.split()
|
words = passphrase.split()
|
||||||
if extended:
|
if extended:
|
||||||
words = [''.join(sorted(w)) for w in words]
|
words = ["".join(sorted(w)) for w in words]
|
||||||
return sorted(list(set(words))) == sorted(words)
|
return sorted(list(set(words))) == sorted(words)
|
||||||
|
|
||||||
def solve(self, puzzle_input):
|
def solve(self, puzzle_input):
|
||||||
|
|
@ -20,6 +20,6 @@ class Solution(BaseSolution):
|
||||||
return sum(self.validate(p, True) for p in puzzle_input.splitlines())
|
return sum(self.validate(p, True) for p in puzzle_input.splitlines())
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
solution = Solution()
|
solution = Solution()
|
||||||
solution.show_results()
|
solution.show_results()
|
||||||
|
|
|
||||||
|
|
@ -2,10 +2,10 @@ from solutions import BaseSolution
|
||||||
|
|
||||||
|
|
||||||
class Solution(BaseSolution):
|
class Solution(BaseSolution):
|
||||||
input_file = '05.txt'
|
input_file = "05.txt"
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return 'Day 5: A Maze of Twisty Trampolines, All Alike'
|
return "Day 5: A Maze of Twisty Trampolines, All Alike"
|
||||||
|
|
||||||
def solve(self, puzzle_input, strange_jumps=False):
|
def solve(self, puzzle_input, strange_jumps=False):
|
||||||
pos = 0
|
pos = 0
|
||||||
|
|
@ -23,6 +23,6 @@ class Solution(BaseSolution):
|
||||||
return self.solve(puzzle_input, True)
|
return self.solve(puzzle_input, True)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
solution = Solution()
|
solution = Solution()
|
||||||
solution.show_results()
|
solution.show_results()
|
||||||
|
|
|
||||||
|
|
@ -2,10 +2,10 @@ from solutions import BaseSolution
|
||||||
|
|
||||||
|
|
||||||
class Solution(BaseSolution):
|
class Solution(BaseSolution):
|
||||||
input_file = '06.txt'
|
input_file = "06.txt"
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return 'Day 6: Memory Reallocation'
|
return "Day 6: Memory Reallocation"
|
||||||
|
|
||||||
def redistribute(self, banks):
|
def redistribute(self, banks):
|
||||||
banks = list(banks)
|
banks = list(banks)
|
||||||
|
|
@ -39,10 +39,10 @@ class Solution(BaseSolution):
|
||||||
|
|
||||||
def solve_again(self, puzzle_input):
|
def solve_again(self, puzzle_input):
|
||||||
seen = self._allocate(puzzle_input)
|
seen = self._allocate(puzzle_input)
|
||||||
seen_last = ' '.join(str(n) for n in seen[-1])
|
seen_last = " ".join(str(n) for n in seen[-1])
|
||||||
return len(self._allocate(seen_last)) - 1
|
return len(self._allocate(seen_last)) - 1
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
solution = Solution()
|
solution = Solution()
|
||||||
solution.show_results()
|
solution.show_results()
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,7 @@ class Program:
|
||||||
except ValueError:
|
except ValueError:
|
||||||
name, weight, _, *disc = data.split()
|
name, weight, _, *disc = data.split()
|
||||||
weight = int(weight[1 : len(weight) - 1])
|
weight = int(weight[1 : len(weight) - 1])
|
||||||
disc = tuple(p.replace(',', '') for p in disc)
|
disc = tuple(p.replace(",", "") for p in disc)
|
||||||
return name, weight, disc
|
return name, weight, disc
|
||||||
|
|
||||||
def has_disc(self):
|
def has_disc(self):
|
||||||
|
|
@ -30,29 +30,31 @@ class Program:
|
||||||
|
|
||||||
|
|
||||||
class Solution(BaseSolution):
|
class Solution(BaseSolution):
|
||||||
input_file = '07.txt'
|
input_file = "07.txt"
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return 'Day 7: Recursive Circus'
|
return "Day 7: Recursive Circus"
|
||||||
|
|
||||||
def _get_programs(self, puzzle_input):
|
def _get_programs(self, puzzle_input):
|
||||||
return [Program(data) for data in puzzle_input.splitlines()]
|
return [Program(data) for data in puzzle_input.splitlines()]
|
||||||
|
|
||||||
def _get_discs(self, disc, programs):
|
def _get_discs(self, disc, programs):
|
||||||
subdisc = [{'own_weight': p.weight, 'obj': p} for p in programs if p.name in disc]
|
subdisc = [
|
||||||
|
{"own_weight": p.weight, "obj": p} for p in programs if p.name in disc
|
||||||
|
]
|
||||||
for t in subdisc:
|
for t in subdisc:
|
||||||
t['weight'] = t['own_weight']
|
t["weight"] = t["own_weight"]
|
||||||
if t['obj'].has_disc():
|
if t["obj"].has_disc():
|
||||||
t['disc'] = self._get_discs(t['obj'].disc, programs)
|
t["disc"] = self._get_discs(t["obj"].disc, programs)
|
||||||
t['weight'] += sum([st['weight'] for st in t['disc']])
|
t["weight"] += sum([st["weight"] for st in t["disc"]])
|
||||||
del (t['obj'])
|
del t["obj"]
|
||||||
return subdisc
|
return subdisc
|
||||||
|
|
||||||
def _find_unbalanced_disc(self, disc):
|
def _find_unbalanced_disc(self, disc):
|
||||||
disc = sorted(disc, key=lambda t: t['weight'])
|
disc = sorted(disc, key=lambda t: t["weight"])
|
||||||
if not disc[0]['weight'] < disc[1]['weight']:
|
if not disc[0]["weight"] < disc[1]["weight"]:
|
||||||
disc = sorted(disc, key=lambda t: t['weight'], reverse=True)
|
disc = sorted(disc, key=lambda t: t["weight"], reverse=True)
|
||||||
return disc[0], disc[1]['weight'] - disc[0]['weight']
|
return disc[0], disc[1]["weight"] - disc[0]["weight"]
|
||||||
|
|
||||||
def solve(self, puzzle_input):
|
def solve(self, puzzle_input):
|
||||||
programs = self._get_programs(puzzle_input)
|
programs = self._get_programs(puzzle_input)
|
||||||
|
|
@ -67,28 +69,30 @@ class Solution(BaseSolution):
|
||||||
unseen = p.unseen_discs(seen)
|
unseen = p.unseen_discs(seen)
|
||||||
if len(unseen) > 0:
|
if len(unseen) > 0:
|
||||||
seen += unseen
|
seen += unseen
|
||||||
bottom_program = list(filter(lambda p: p.name not in seen, programs_with_discs))[0]
|
bottom_program = list(
|
||||||
|
filter(lambda p: p.name not in seen, programs_with_discs)
|
||||||
|
)[0]
|
||||||
return bottom_program
|
return bottom_program
|
||||||
|
|
||||||
def solve_again(self, puzzle_input):
|
def solve_again(self, puzzle_input):
|
||||||
programs = self._get_programs(puzzle_input)
|
programs = self._get_programs(puzzle_input)
|
||||||
bottom_program = self.solve(puzzle_input)
|
bottom_program = self.solve(puzzle_input)
|
||||||
disc_tree = {
|
disc_tree = {
|
||||||
'own_weight': bottom_program.weight,
|
"own_weight": bottom_program.weight,
|
||||||
'disc': self._get_discs(bottom_program.disc, programs)
|
"disc": self._get_discs(bottom_program.disc, programs),
|
||||||
}
|
}
|
||||||
diff = -1
|
diff = -1
|
||||||
unbalanced = True
|
unbalanced = True
|
||||||
while unbalanced:
|
while unbalanced:
|
||||||
disc, new_diff = self._find_unbalanced_disc(disc_tree['disc'])
|
disc, new_diff = self._find_unbalanced_disc(disc_tree["disc"])
|
||||||
if new_diff == 0:
|
if new_diff == 0:
|
||||||
unbalanced = False
|
unbalanced = False
|
||||||
else:
|
else:
|
||||||
disc_tree = disc
|
disc_tree = disc
|
||||||
diff = new_diff
|
diff = new_diff
|
||||||
return disc_tree['own_weight'] + diff
|
return disc_tree["own_weight"] + diff
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
solution = Solution()
|
solution = Solution()
|
||||||
solution.show_results()
|
solution.show_results()
|
||||||
|
|
|
||||||
|
|
@ -2,22 +2,22 @@ from solutions import BaseSolution
|
||||||
|
|
||||||
|
|
||||||
class Solution(BaseSolution):
|
class Solution(BaseSolution):
|
||||||
input_file = '08.txt'
|
input_file = "08.txt"
|
||||||
registry = {}
|
registry = {}
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return 'Day 8: I Heard You Like Registers'
|
return "Day 8: I Heard You Like Registers"
|
||||||
|
|
||||||
def _should_modify(self, x, comp, y):
|
def _should_modify(self, x, comp, y):
|
||||||
if comp in ('==', '!=', '>=', '<=', '>', '<'):
|
if comp in ("==", "!=", ">=", "<=", ">", "<"):
|
||||||
return eval('{:d} {} {:d}'.format(x, comp, y))
|
return eval("{:d} {} {:d}".format(x, comp, y))
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def _update_registry(self, registry, instruction):
|
def _update_registry(self, registry, instruction):
|
||||||
r, action, n, _, k, comp, v = instruction.split()
|
r, action, n, _, k, comp, v = instruction.split()
|
||||||
current = registry.get(r, 0)
|
current = registry.get(r, 0)
|
||||||
if self._should_modify(registry.get(k, 0), comp, int(v)):
|
if self._should_modify(registry.get(k, 0), comp, int(v)):
|
||||||
registry[r] = current + int(n) if action == 'inc' else current - int(n)
|
registry[r] = current + int(n) if action == "inc" else current - int(n)
|
||||||
|
|
||||||
def solve(self, puzzle_input):
|
def solve(self, puzzle_input):
|
||||||
registry = {}
|
registry = {}
|
||||||
|
|
@ -35,6 +35,6 @@ class Solution(BaseSolution):
|
||||||
return max_value
|
return max_value
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
solution = Solution()
|
solution = Solution()
|
||||||
solution.show_results()
|
solution.show_results()
|
||||||
|
|
|
||||||
|
|
@ -4,30 +4,30 @@ from solutions import BaseSolution
|
||||||
|
|
||||||
|
|
||||||
class Solution(BaseSolution):
|
class Solution(BaseSolution):
|
||||||
input_file = '09.txt'
|
input_file = "09.txt"
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return 'Day 9: Stream Processing'
|
return "Day 9: Stream Processing"
|
||||||
|
|
||||||
def solve(self, puzzle_input):
|
def solve(self, puzzle_input):
|
||||||
level = 0
|
level = 0
|
||||||
groups = []
|
groups = []
|
||||||
stream = re.sub(r'!.', '', puzzle_input)
|
stream = re.sub(r"!.", "", puzzle_input)
|
||||||
stream = re.sub(r'<[^>]*>', '', stream)
|
stream = re.sub(r"<[^>]*>", "", stream)
|
||||||
for c in stream:
|
for c in stream:
|
||||||
if c == '{':
|
if c == "{":
|
||||||
level += 1
|
level += 1
|
||||||
if c == '}':
|
if c == "}":
|
||||||
groups.append(level)
|
groups.append(level)
|
||||||
level -= 1
|
level -= 1
|
||||||
return sum(groups)
|
return sum(groups)
|
||||||
|
|
||||||
def solve_again(self, puzzle_input):
|
def solve_again(self, puzzle_input):
|
||||||
stream = re.sub(r'!.', '', puzzle_input)
|
stream = re.sub(r"!.", "", puzzle_input)
|
||||||
garbage = re.findall(r'<([^>]*)>', stream)
|
garbage = re.findall(r"<([^>]*)>", stream)
|
||||||
return sum([len(g) for g in garbage])
|
return sum([len(g) for g in garbage])
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
solution = Solution()
|
solution = Solution()
|
||||||
solution.show_results()
|
solution.show_results()
|
||||||
|
|
|
||||||
|
|
@ -2,14 +2,14 @@ from solutions import BaseSolution
|
||||||
|
|
||||||
|
|
||||||
class Solution(BaseSolution):
|
class Solution(BaseSolution):
|
||||||
input_file = '10.txt'
|
input_file = "10.txt"
|
||||||
list = []
|
list = []
|
||||||
l = 0
|
l = 0
|
||||||
skip_size = 0
|
skip_size = 0
|
||||||
pos = 0
|
pos = 0
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return 'Day 10: Knot Hash'
|
return "Day 10: Knot Hash"
|
||||||
|
|
||||||
def reset(self, l=256):
|
def reset(self, l=256):
|
||||||
self.list = list(range(l))
|
self.list = list(range(l))
|
||||||
|
|
@ -28,7 +28,7 @@ class Solution(BaseSolution):
|
||||||
|
|
||||||
def solve(self, puzzle_input, r=256):
|
def solve(self, puzzle_input, r=256):
|
||||||
self.reset(r)
|
self.reset(r)
|
||||||
for sublist_length in map(int, puzzle_input.split(',')):
|
for sublist_length in map(int, puzzle_input.split(",")):
|
||||||
self.reverse(sublist_length)
|
self.reverse(sublist_length)
|
||||||
return self.list[0] * self.list[1]
|
return self.list[0] * self.list[1]
|
||||||
|
|
||||||
|
|
@ -38,10 +38,13 @@ class Solution(BaseSolution):
|
||||||
for _ in range(64):
|
for _ in range(64):
|
||||||
for sublist_length in puzzle_input:
|
for sublist_length in puzzle_input:
|
||||||
self.reverse(sublist_length)
|
self.reverse(sublist_length)
|
||||||
dense_hash = [eval('^'.join(list(map(str, self.list[seq:seq+16])))) for seq in range(0, r, 16)]
|
dense_hash = [
|
||||||
return ''.join(['{:x}'.format(int(i)).zfill(2) for i in dense_hash])
|
eval("^".join(list(map(str, self.list[seq : seq + 16]))))
|
||||||
|
for seq in range(0, r, 16)
|
||||||
|
]
|
||||||
|
return "".join(["{:x}".format(int(i)).zfill(2) for i in dense_hash])
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
solution = Solution()
|
solution = Solution()
|
||||||
solution.show_results()
|
solution.show_results()
|
||||||
|
|
|
||||||
|
|
@ -2,20 +2,20 @@ from solutions import BaseSolution
|
||||||
|
|
||||||
|
|
||||||
class Solution(BaseSolution):
|
class Solution(BaseSolution):
|
||||||
input_file = '11.txt'
|
input_file = "11.txt"
|
||||||
furthest_away = 0
|
furthest_away = 0
|
||||||
# https://www.redblobgames.com/grids/hexagons/#coordinates
|
# https://www.redblobgames.com/grids/hexagons/#coordinates
|
||||||
DIRECTIONS = {
|
DIRECTIONS = {
|
||||||
'n': lambda x, y, z: (x, y + 1, z - 1),
|
"n": lambda x, y, z: (x, y + 1, z - 1),
|
||||||
'ne': lambda x, y, z: (x + 1, y, z - 1),
|
"ne": lambda x, y, z: (x + 1, y, z - 1),
|
||||||
'se': lambda x, y, z: (x + 1, y - 1, z),
|
"se": lambda x, y, z: (x + 1, y - 1, z),
|
||||||
's': lambda x, y, z: (x, y - 1, z + 1),
|
"s": lambda x, y, z: (x, y - 1, z + 1),
|
||||||
'sw': lambda x, y, z: (x - 1, y, z + 1),
|
"sw": lambda x, y, z: (x - 1, y, z + 1),
|
||||||
'nw': lambda x, y, z: (x - 1, y + 1, z),
|
"nw": lambda x, y, z: (x - 1, y + 1, z),
|
||||||
}
|
}
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return 'Day 11: Hex Ed'
|
return "Day 11: Hex Ed"
|
||||||
|
|
||||||
def _get_end(self, steps):
|
def _get_end(self, steps):
|
||||||
x = 0
|
x = 0
|
||||||
|
|
@ -28,14 +28,14 @@ class Solution(BaseSolution):
|
||||||
return abs(x), abs(y), abs(z)
|
return abs(x), abs(y), abs(z)
|
||||||
|
|
||||||
def solve(self, puzzle_input):
|
def solve(self, puzzle_input):
|
||||||
x, y, z = self._get_end(puzzle_input.split(','))
|
x, y, z = self._get_end(puzzle_input.split(","))
|
||||||
return max(x, y, z)
|
return max(x, y, z)
|
||||||
|
|
||||||
def solve_again(self, puzzle_input):
|
def solve_again(self, puzzle_input):
|
||||||
_, *__ = self._get_end(puzzle_input.split(','))
|
_, *__ = self._get_end(puzzle_input.split(","))
|
||||||
return self.furthest_away
|
return self.furthest_away
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
solution = Solution()
|
solution = Solution()
|
||||||
solution.show_results()
|
solution.show_results()
|
||||||
|
|
|
||||||
|
|
@ -2,17 +2,17 @@ from solutions import BaseSolution
|
||||||
|
|
||||||
|
|
||||||
class Solution(BaseSolution):
|
class Solution(BaseSolution):
|
||||||
input_file = '12.txt'
|
input_file = "12.txt"
|
||||||
seen = []
|
seen = []
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return 'Day 12: Digital Plumber'
|
return "Day 12: Digital Plumber"
|
||||||
|
|
||||||
def _walk(self, i, programs):
|
def _walk(self, i, programs):
|
||||||
line = next(filter(lambda l: l.startswith('{} <->'.format(i)), programs))
|
line = next(filter(lambda l: l.startswith("{} <->".format(i)), programs))
|
||||||
piped = line.split()[2:]
|
piped = line.split()[2:]
|
||||||
self.seen.add(i)
|
self.seen.add(i)
|
||||||
for p in [int(p.replace(',', '')) for p in piped]:
|
for p in [int(p.replace(",", "")) for p in piped]:
|
||||||
if p not in self.seen:
|
if p not in self.seen:
|
||||||
self._walk(p, programs)
|
self._walk(p, programs)
|
||||||
|
|
||||||
|
|
@ -34,6 +34,6 @@ class Solution(BaseSolution):
|
||||||
return groups
|
return groups
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
solution = Solution()
|
solution = Solution()
|
||||||
solution.show_results()
|
solution.show_results()
|
||||||
|
|
|
||||||
|
|
@ -4,12 +4,12 @@ from solutions import BaseSolution
|
||||||
|
|
||||||
|
|
||||||
class Solution(BaseSolution):
|
class Solution(BaseSolution):
|
||||||
input_file = '13.txt'
|
input_file = "13.txt"
|
||||||
layers = []
|
layers = []
|
||||||
scanners = []
|
scanners = []
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return 'Day 13: Packet Scanners'
|
return "Day 13: Packet Scanners"
|
||||||
|
|
||||||
def _move_scanners(self):
|
def _move_scanners(self):
|
||||||
for p, l in enumerate(self.layers):
|
for p, l in enumerate(self.layers):
|
||||||
|
|
@ -22,7 +22,10 @@ class Solution(BaseSolution):
|
||||||
self.scanners = [[0, 1] for l in self.layers]
|
self.scanners = [[0, 1] for l in self.layers]
|
||||||
|
|
||||||
def _setup(self, puzzle_input):
|
def _setup(self, puzzle_input):
|
||||||
pi = [tuple(map(int, line.strip().split(': '))) for line in puzzle_input.splitlines()]
|
pi = [
|
||||||
|
tuple(map(int, line.strip().split(": ")))
|
||||||
|
for line in puzzle_input.splitlines()
|
||||||
|
]
|
||||||
self.layers = [0 for _ in range(pi[-1][0] + 1)]
|
self.layers = [0 for _ in range(pi[-1][0] + 1)]
|
||||||
self._init_scanners()
|
self._init_scanners()
|
||||||
for k, v in pi:
|
for k, v in pi:
|
||||||
|
|
@ -55,13 +58,16 @@ class Solution(BaseSolution):
|
||||||
|
|
||||||
def solve_again(self, puzzle_input):
|
def solve_again(self, puzzle_input):
|
||||||
# todo: rewrite!
|
# todo: rewrite!
|
||||||
lines = [line.split(': ') for line in puzzle_input.splitlines()]
|
lines = [line.split(": ") for line in puzzle_input.splitlines()]
|
||||||
heights = {int(pos): int(height) for pos, height in lines}
|
heights = {int(pos): int(height) for pos, height in lines}
|
||||||
wait = next(
|
wait = next(
|
||||||
wait for wait in itertools.count() if not any(self._scan(heights[pos], wait + pos) == 0 for pos in heights))
|
wait
|
||||||
|
for wait in itertools.count()
|
||||||
|
if not any(self._scan(heights[pos], wait + pos) == 0 for pos in heights)
|
||||||
|
)
|
||||||
return wait
|
return wait
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
solution = Solution()
|
solution = Solution()
|
||||||
solution.show_results()
|
solution.show_results()
|
||||||
|
|
|
||||||
|
|
@ -3,18 +3,18 @@ from solutions.day_10 import Solution as KnotHash
|
||||||
|
|
||||||
|
|
||||||
class Solution(BaseSolution):
|
class Solution(BaseSolution):
|
||||||
input_file = '14.txt'
|
input_file = "14.txt"
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return 'Day 14: Disk Defragmentation'
|
return "Day 14: Disk Defragmentation"
|
||||||
|
|
||||||
def _get_grid(self, pi):
|
def _get_grid(self, pi):
|
||||||
grid = ''
|
grid = ""
|
||||||
ks = KnotHash()
|
ks = KnotHash()
|
||||||
for n in range(128):
|
for n in range(128):
|
||||||
s = '-'.join([pi, str(n)])
|
s = "-".join([pi, str(n)])
|
||||||
knothash = ks.solve_again(s)
|
knothash = ks.solve_again(s)
|
||||||
grid += '{:0128b}'.format(int(knothash, 16))
|
grid += "{:0128b}".format(int(knothash, 16))
|
||||||
return grid
|
return grid
|
||||||
|
|
||||||
def _find_regions(self, squares):
|
def _find_regions(self, squares):
|
||||||
|
|
@ -47,10 +47,10 @@ class Solution(BaseSolution):
|
||||||
|
|
||||||
def solve_again(self, puzzle_input):
|
def solve_again(self, puzzle_input):
|
||||||
grid = self._get_grid(puzzle_input)
|
grid = self._get_grid(puzzle_input)
|
||||||
squares = [i for i, s in enumerate(list(grid)) if s == '1']
|
squares = [i for i, s in enumerate(list(grid)) if s == "1"]
|
||||||
return self._find_regions(squares)
|
return self._find_regions(squares)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
solution = Solution()
|
solution = Solution()
|
||||||
solution.show_results()
|
solution.show_results()
|
||||||
|
|
|
||||||
|
|
@ -2,10 +2,10 @@ from solutions import BaseSolution
|
||||||
|
|
||||||
|
|
||||||
class Solution(BaseSolution):
|
class Solution(BaseSolution):
|
||||||
input_file = '15.txt'
|
input_file = "15.txt"
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return 'Day 15: Dueling Generators'
|
return "Day 15: Dueling Generators"
|
||||||
|
|
||||||
def _calc(self, x, f, m=1):
|
def _calc(self, x, f, m=1):
|
||||||
x = (x * f) % 2147483647
|
x = (x * f) % 2147483647
|
||||||
|
|
@ -21,7 +21,7 @@ class Solution(BaseSolution):
|
||||||
for _ in range(40 * 10**6):
|
for _ in range(40 * 10**6):
|
||||||
a = self._calc(a, af)
|
a = self._calc(a, af)
|
||||||
b = self._calc(b, bf)
|
b = self._calc(b, bf)
|
||||||
if '{:b}'.format(a)[-16:] == '{:b}'.format(b)[-16:]:
|
if "{:b}".format(a)[-16:] == "{:b}".format(b)[-16:]:
|
||||||
j += 1
|
j += 1
|
||||||
return j
|
return j
|
||||||
|
|
||||||
|
|
@ -32,11 +32,11 @@ class Solution(BaseSolution):
|
||||||
for _ in range(5 * 10**6):
|
for _ in range(5 * 10**6):
|
||||||
a = self._calc(a, af, 4)
|
a = self._calc(a, af, 4)
|
||||||
b = self._calc(b, bf, 8)
|
b = self._calc(b, bf, 8)
|
||||||
if '{:b}'.format(a)[-16:] == '{:b}'.format(b)[-16:]:
|
if "{:b}".format(a)[-16:] == "{:b}".format(b)[-16:]:
|
||||||
j += 1
|
j += 1
|
||||||
return j
|
return j
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
solution = Solution()
|
solution = Solution()
|
||||||
solution.show_results()
|
solution.show_results()
|
||||||
|
|
|
||||||
|
|
@ -2,24 +2,24 @@ from solutions import BaseSolution
|
||||||
|
|
||||||
|
|
||||||
class Solution(BaseSolution):
|
class Solution(BaseSolution):
|
||||||
input_file = '16.txt'
|
input_file = "16.txt"
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return 'Day 16: Permutation Promenade'
|
return "Day 16: Permutation Promenade"
|
||||||
|
|
||||||
def _move(self, programs, m, i):
|
def _move(self, programs, m, i):
|
||||||
l = len(programs)
|
l = len(programs)
|
||||||
if m == 's':
|
if m == "s":
|
||||||
r = int(i)
|
r = int(i)
|
||||||
return programs[-r:] + programs[: l - r]
|
return programs[-r:] + programs[: l - r]
|
||||||
if m == 'x':
|
if m == "x":
|
||||||
x, y = [int(s) for s in i.split('/')]
|
x, y = [int(s) for s in i.split("/")]
|
||||||
z = programs[x]
|
z = programs[x]
|
||||||
programs[x] = programs[y]
|
programs[x] = programs[y]
|
||||||
programs[y] = z
|
programs[y] = z
|
||||||
return programs
|
return programs
|
||||||
if m == 'p':
|
if m == "p":
|
||||||
xp, yp = i.split('/')
|
xp, yp = i.split("/")
|
||||||
x = programs.index(xp)
|
x = programs.index(xp)
|
||||||
y = programs.index(yp)
|
y = programs.index(yp)
|
||||||
z = programs[x]
|
z = programs[x]
|
||||||
|
|
@ -34,11 +34,11 @@ class Solution(BaseSolution):
|
||||||
|
|
||||||
def solve(self, puzzle_input, n=16):
|
def solve(self, puzzle_input, n=16):
|
||||||
programs = [chr(c) for c in range(97, 97 + n)]
|
programs = [chr(c) for c in range(97, 97 + n)]
|
||||||
moves = puzzle_input.split(',')
|
moves = puzzle_input.split(",")
|
||||||
return ''.join(self._dance(programs, moves))
|
return "".join(self._dance(programs, moves))
|
||||||
|
|
||||||
def solve_again(self, puzzle_input, n=16):
|
def solve_again(self, puzzle_input, n=16):
|
||||||
moves = puzzle_input.split(',')
|
moves = puzzle_input.split(",")
|
||||||
initial = [chr(c) for c in range(97, 97 + n)]
|
initial = [chr(c) for c in range(97, 97 + n)]
|
||||||
programs = list(self.solve(puzzle_input))
|
programs = list(self.solve(puzzle_input))
|
||||||
dances = 1
|
dances = 1
|
||||||
|
|
@ -47,9 +47,9 @@ class Solution(BaseSolution):
|
||||||
dances += 1
|
dances += 1
|
||||||
for _ in range(10**9 % dances):
|
for _ in range(10**9 % dances):
|
||||||
programs = self._dance(programs, moves)
|
programs = self._dance(programs, moves)
|
||||||
return ''.join(programs)
|
return "".join(programs)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
solution = Solution()
|
solution = Solution()
|
||||||
solution.show_results()
|
solution.show_results()
|
||||||
|
|
|
||||||
|
|
@ -2,10 +2,10 @@ from solutions import BaseSolution
|
||||||
|
|
||||||
|
|
||||||
class Solution(BaseSolution):
|
class Solution(BaseSolution):
|
||||||
input_file = '17.txt'
|
input_file = "17.txt"
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return 'Day 17: Spinlock'
|
return "Day 17: Spinlock"
|
||||||
|
|
||||||
def solve(self, puzzle_input):
|
def solve(self, puzzle_input):
|
||||||
n = int(puzzle_input)
|
n = int(puzzle_input)
|
||||||
|
|
@ -27,6 +27,6 @@ class Solution(BaseSolution):
|
||||||
return last_seen
|
return last_seen
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
solution = Solution()
|
solution = Solution()
|
||||||
solution.show_results()
|
solution.show_results()
|
||||||
|
|
|
||||||
|
|
@ -2,16 +2,16 @@ from solutions import BaseSolution
|
||||||
|
|
||||||
|
|
||||||
class Solution(BaseSolution):
|
class Solution(BaseSolution):
|
||||||
input_file = '18.txt'
|
input_file = "18.txt"
|
||||||
sound_freq = 0
|
sound_freq = 0
|
||||||
queue = [], []
|
queue = [], []
|
||||||
sent = [0, 0]
|
sent = [0, 0]
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return 'Day 18: Duet'
|
return "Day 18: Duet"
|
||||||
|
|
||||||
def _run(self, line, registry, swag_mode=True):
|
def _run(self, line, registry, swag_mode=True):
|
||||||
actions = 'add jgz mod mul rcv set snd'
|
actions = "add jgz mod mul rcv set snd"
|
||||||
a, *kv = line.split()
|
a, *kv = line.split()
|
||||||
if len(kv) == 2:
|
if len(kv) == 2:
|
||||||
k, v = kv
|
k, v = kv
|
||||||
|
|
@ -19,9 +19,9 @@ class Solution(BaseSolution):
|
||||||
k = kv[0]
|
k = kv[0]
|
||||||
v = None
|
v = None
|
||||||
if a in actions:
|
if a in actions:
|
||||||
if a == 'add' and k in registry:
|
if a == "add" and k in registry:
|
||||||
registry[k] += registry[v] if v in registry else int(v)
|
registry[k] += registry[v] if v in registry else int(v)
|
||||||
if a == 'jgz': # damn you, 'jgz 1 3'
|
if a == "jgz": # damn you, 'jgz 1 3'
|
||||||
try:
|
try:
|
||||||
k = int(k)
|
k = int(k)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
|
|
@ -31,25 +31,25 @@ class Solution(BaseSolution):
|
||||||
except ValueError:
|
except ValueError:
|
||||||
v = registry[v] if v in registry else 1
|
v = registry[v] if v in registry else 1
|
||||||
return v if k > 0 else 1
|
return v if k > 0 else 1
|
||||||
if a == 'mod' and k in registry:
|
if a == "mod" and k in registry:
|
||||||
registry[k] %= registry[v] if v in registry else int(v)
|
registry[k] %= registry[v] if v in registry else int(v)
|
||||||
if a == 'mul' and k in registry:
|
if a == "mul" and k in registry:
|
||||||
registry[k] *= registry[v] if v in registry else int(v)
|
registry[k] *= registry[v] if v in registry else int(v)
|
||||||
if a == 'set':
|
if a == "set":
|
||||||
registry[k] = registry[v] if v in registry else int(v)
|
registry[k] = registry[v] if v in registry else int(v)
|
||||||
if swag_mode: # Part 1: scientific wild-ass guess
|
if swag_mode: # Part 1: scientific wild-ass guess
|
||||||
if a == 'rcv' and registry[k] != 0:
|
if a == "rcv" and registry[k] != 0:
|
||||||
return self.STOP_SIGNAL
|
return self.STOP_SIGNAL
|
||||||
if a == 'snd' and k in registry:
|
if a == "snd" and k in registry:
|
||||||
self.sound_freq = registry[k]
|
self.sound_freq = registry[k]
|
||||||
else: # part 2, actual instructions
|
else: # part 2, actual instructions
|
||||||
if a == 'rcv':
|
if a == "rcv":
|
||||||
if len(self.queue[registry['_id']]) == 0:
|
if len(self.queue[registry["_id"]]) == 0:
|
||||||
return 0
|
return 0
|
||||||
registry[k] = self.queue[registry['_id']].pop(0)
|
registry[k] = self.queue[registry["_id"]].pop(0)
|
||||||
if a == 'snd':
|
if a == "snd":
|
||||||
self.sent[registry['_id']] += 1
|
self.sent[registry["_id"]] += 1
|
||||||
q = (registry['_id'] + 1) % 2
|
q = (registry["_id"] + 1) % 2
|
||||||
kk = registry[k] if k in registry else int(k)
|
kk = registry[k] if k in registry else int(k)
|
||||||
self.queue[q].append(kk)
|
self.queue[q].append(kk)
|
||||||
return 1
|
return 1
|
||||||
|
|
@ -65,7 +65,7 @@ class Solution(BaseSolution):
|
||||||
return self.sound_freq
|
return self.sound_freq
|
||||||
|
|
||||||
def solve_again(self, puzzle_input):
|
def solve_again(self, puzzle_input):
|
||||||
registry = {'p': 0, '_id': 0}, {'p': 1, '_id': 1}
|
registry = {"p": 0, "_id": 0}, {"p": 1, "_id": 1}
|
||||||
lines = puzzle_input.splitlines()
|
lines = puzzle_input.splitlines()
|
||||||
i = 0
|
i = 0
|
||||||
j = 0
|
j = 0
|
||||||
|
|
@ -89,6 +89,6 @@ class Solution(BaseSolution):
|
||||||
return self.sent[1]
|
return self.sent[1]
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
solution = Solution()
|
solution = Solution()
|
||||||
solution.show_results()
|
solution.show_results()
|
||||||
|
|
|
||||||
|
|
@ -2,48 +2,48 @@ from solutions import BaseSolution
|
||||||
|
|
||||||
|
|
||||||
class Solution(BaseSolution):
|
class Solution(BaseSolution):
|
||||||
input_file = '19.txt'
|
input_file = "19.txt"
|
||||||
trim_input = False
|
trim_input = False
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return 'Day 19: A Series of Tubes'
|
return "Day 19: A Series of Tubes"
|
||||||
|
|
||||||
def _walk_maze(self, puzzle_input):
|
def _walk_maze(self, puzzle_input):
|
||||||
DIRECTIONS = {
|
DIRECTIONS = {
|
||||||
'D': (1, 0),
|
"D": (1, 0),
|
||||||
'U': (-1, 0),
|
"U": (-1, 0),
|
||||||
'R': (0, 1),
|
"R": (0, 1),
|
||||||
'L': (0, -1),
|
"L": (0, -1),
|
||||||
}
|
}
|
||||||
maze = puzzle_input.splitlines()
|
maze = puzzle_input.splitlines()
|
||||||
pos = (0, list(maze[0]).index('|'))
|
pos = (0, list(maze[0]).index("|"))
|
||||||
d = DIRECTIONS['D']
|
d = DIRECTIONS["D"]
|
||||||
paths = '-|'
|
paths = "-|"
|
||||||
steps = 0
|
steps = 0
|
||||||
seen = ''
|
seen = ""
|
||||||
|
|
||||||
def _nc(nu):
|
def _nc(nu):
|
||||||
np = (pos[0] + nu[0], pos[1] + nu[1])
|
np = (pos[0] + nu[0], pos[1] + nu[1])
|
||||||
if np[0] < len(maze) and np[1] < len(maze[np[0]]):
|
if np[0] < len(maze) and np[1] < len(maze[np[0]]):
|
||||||
return maze[np[0]][np[1]]
|
return maze[np[0]][np[1]]
|
||||||
else:
|
else:
|
||||||
return ' '
|
return " "
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
pos = (pos[0] + d[0], pos[1] + d[1])
|
pos = (pos[0] + d[0], pos[1] + d[1])
|
||||||
c = _nc((0, 0))
|
c = _nc((0, 0))
|
||||||
steps += 1
|
steps += 1
|
||||||
if c == '+':
|
if c == "+":
|
||||||
nc = _nc(d)
|
nc = _nc(d)
|
||||||
if nc == ' ':
|
if nc == " ":
|
||||||
for v in DIRECTIONS.values():
|
for v in DIRECTIONS.values():
|
||||||
if -v[0] == d[0] and -v[1] == d[1]:
|
if -v[0] == d[0] and -v[1] == d[1]:
|
||||||
continue
|
continue
|
||||||
nc = _nc(v)
|
nc = _nc(v)
|
||||||
if nc != ' ':
|
if nc != " ":
|
||||||
d = v
|
d = v
|
||||||
break
|
break
|
||||||
elif c == ' ':
|
elif c == " ":
|
||||||
break
|
break
|
||||||
elif c not in paths:
|
elif c not in paths:
|
||||||
seen += c
|
seen += c
|
||||||
|
|
@ -58,6 +58,6 @@ class Solution(BaseSolution):
|
||||||
return steps
|
return steps
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
solution = Solution()
|
solution = Solution()
|
||||||
solution.show_results()
|
solution.show_results()
|
||||||
|
|
|
||||||
|
|
@ -6,13 +6,16 @@ from solutions import BaseSolution
|
||||||
|
|
||||||
|
|
||||||
class Solution(BaseSolution):
|
class Solution(BaseSolution):
|
||||||
input_file = '20.txt'
|
input_file = "20.txt"
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return 'Day 20: Particle Swarm'
|
return "Day 20: Particle Swarm"
|
||||||
|
|
||||||
def _get_particle(self, line):
|
def _get_particle(self, line):
|
||||||
return [list(map(int, coordinate.split(','))) for coordinate in re.findall(r'.=<([^>]+)>', line)]
|
return [
|
||||||
|
list(map(int, coordinate.split(",")))
|
||||||
|
for coordinate in re.findall(r".=<([^>]+)>", line)
|
||||||
|
]
|
||||||
|
|
||||||
def solve(self, puzzle_input, ticks=10**4):
|
def solve(self, puzzle_input, ticks=10**4):
|
||||||
particles = [self._get_particle(line) for line in puzzle_input.splitlines()]
|
particles = [self._get_particle(line) for line in puzzle_input.splitlines()]
|
||||||
|
|
@ -27,7 +30,10 @@ class Solution(BaseSolution):
|
||||||
p[1] += v[1]
|
p[1] += v[1]
|
||||||
p[2] += v[2]
|
p[2] += v[2]
|
||||||
distances[i].append(sum(map(abs, p)))
|
distances[i].append(sum(map(abs, p)))
|
||||||
d = sorted(map(lambda d: (d[0], sum(d[1]) / len(d[1])), enumerate(distances)), key=lambda x: x[1])
|
d = sorted(
|
||||||
|
map(lambda d: (d[0], sum(d[1]) / len(d[1])), enumerate(distances)),
|
||||||
|
key=lambda x: x[1],
|
||||||
|
)
|
||||||
return d[0][0]
|
return d[0][0]
|
||||||
|
|
||||||
def solve_again(self, puzzle_input, ticks=10**3):
|
def solve_again(self, puzzle_input, ticks=10**3):
|
||||||
|
|
@ -42,7 +48,7 @@ class Solution(BaseSolution):
|
||||||
p[0] += v[0]
|
p[0] += v[0]
|
||||||
p[1] += v[1]
|
p[1] += v[1]
|
||||||
p[2] += v[2]
|
p[2] += v[2]
|
||||||
k = '-'.join(map(str, p))
|
k = "-".join(map(str, p))
|
||||||
positions[k].append(particle)
|
positions[k].append(particle)
|
||||||
for duplicates in positions.values():
|
for duplicates in positions.values():
|
||||||
if len(duplicates) > 1:
|
if len(duplicates) > 1:
|
||||||
|
|
@ -51,6 +57,6 @@ class Solution(BaseSolution):
|
||||||
return len(particles)
|
return len(particles)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
solution = Solution()
|
solution = Solution()
|
||||||
solution.show_results()
|
solution.show_results()
|
||||||
|
|
|
||||||
|
|
@ -7,9 +7,9 @@ class Rule:
|
||||||
size = 0
|
size = 0
|
||||||
|
|
||||||
def __init__(self, line):
|
def __init__(self, line):
|
||||||
pattern, output = line.split(' => ')
|
pattern, output = line.split(" => ")
|
||||||
self.pattern = pattern.replace('/', '')
|
self.pattern = pattern.replace("/", "")
|
||||||
self.output = output.replace('/', '')
|
self.output = output.replace("/", "")
|
||||||
self.size = int(len(self.pattern) ** 0.5)
|
self.size = int(len(self.pattern) ** 0.5)
|
||||||
|
|
||||||
self.patterns = [
|
self.patterns = [
|
||||||
|
|
@ -22,10 +22,15 @@ class Rule:
|
||||||
]
|
]
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return '[{}] {} -> {}'.format(self.size, self.pattern, self.output)
|
return "[{}] {} -> {}".format(self.size, self.pattern, self.output)
|
||||||
|
|
||||||
def _r(self, b):
|
def _r(self, b):
|
||||||
return ''.join(map(lambda g: ''.join(g), zip(*[b[s:s + self.size] for s in range(0, len(b), self.size)][::-1])))
|
return "".join(
|
||||||
|
map(
|
||||||
|
lambda g: "".join(g),
|
||||||
|
zip(*[b[s : s + self.size] for s in range(0, len(b), self.size)][::-1]),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
def _f(self, b):
|
def _f(self, b):
|
||||||
return b[-self.size :] + b[self.size : len(b) - self.size] + b[0 : self.size]
|
return b[-self.size :] + b[self.size : len(b) - self.size] + b[0 : self.size]
|
||||||
|
|
@ -38,13 +43,13 @@ class Rule:
|
||||||
|
|
||||||
|
|
||||||
class Solution(BaseSolution):
|
class Solution(BaseSolution):
|
||||||
input_file = '21.txt'
|
input_file = "21.txt"
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return 'Day 21: Fractal Art'
|
return "Day 21: Fractal Art"
|
||||||
|
|
||||||
def _get_block(self, canvas, size, n=0):
|
def _get_block(self, canvas, size, n=0):
|
||||||
s = ''
|
s = ""
|
||||||
cl = int(len(canvas) ** 0.5)
|
cl = int(len(canvas) ** 0.5)
|
||||||
x = n % (cl // size)
|
x = n % (cl // size)
|
||||||
y = n // (cl // size)
|
y = n // (cl // size)
|
||||||
|
|
@ -58,14 +63,16 @@ class Solution(BaseSolution):
|
||||||
bl = len(blocks)
|
bl = len(blocks)
|
||||||
c = int(bl**0.5)
|
c = int(bl**0.5)
|
||||||
rl = int(len(blocks[0]) ** 0.5)
|
rl = int(len(blocks[0]) ** 0.5)
|
||||||
canvas = ''
|
canvas = ""
|
||||||
for i in range(0, bl, c):
|
for i in range(0, bl, c):
|
||||||
for j in range(rl):
|
for j in range(rl):
|
||||||
canvas += ''.join([block[j * rl:(j + 1) * rl] for block in blocks[i:i + c]])
|
canvas += "".join(
|
||||||
|
[block[j * rl : (j + 1) * rl] for block in blocks[i : i + c]]
|
||||||
|
)
|
||||||
return canvas
|
return canvas
|
||||||
|
|
||||||
def solve(self, puzzle_input, iterations=5):
|
def solve(self, puzzle_input, iterations=5):
|
||||||
canvas = '.#...####'
|
canvas = ".#...####"
|
||||||
rules = [Rule(l.strip()) for l in puzzle_input.splitlines()]
|
rules = [Rule(l.strip()) for l in puzzle_input.splitlines()]
|
||||||
for _ in range(iterations):
|
for _ in range(iterations):
|
||||||
size = 2 if len(canvas) % 2 == 0 else 3
|
size = 2 if len(canvas) % 2 == 0 else 3
|
||||||
|
|
@ -78,12 +85,12 @@ class Solution(BaseSolution):
|
||||||
r = rule[0]
|
r = rule[0]
|
||||||
cb.append(r.enhance(bc))
|
cb.append(r.enhance(bc))
|
||||||
canvas = self._join_blocks(cb)
|
canvas = self._join_blocks(cb)
|
||||||
return collections.Counter(canvas)['#']
|
return collections.Counter(canvas)["#"]
|
||||||
|
|
||||||
def solve_again(self, puzzle_input):
|
def solve_again(self, puzzle_input):
|
||||||
return self.solve(puzzle_input, 18)
|
return self.solve(puzzle_input, 18)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
solution = Solution()
|
solution = Solution()
|
||||||
solution.show_results()
|
solution.show_results()
|
||||||
|
|
|
||||||
|
|
@ -4,10 +4,10 @@ from solutions import BaseSolution
|
||||||
|
|
||||||
|
|
||||||
class Solution(BaseSolution):
|
class Solution(BaseSolution):
|
||||||
input_file = '22.txt'
|
input_file = "22.txt"
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return 'Day 22: Sporifica Virus'
|
return "Day 22: Sporifica Virus"
|
||||||
|
|
||||||
infected = 0
|
infected = 0
|
||||||
|
|
||||||
|
|
@ -50,7 +50,7 @@ class Solution(BaseSolution):
|
||||||
dirs = dirs[1:] + [dirs[0]]
|
dirs = dirs[1:] + [dirs[0]]
|
||||||
elif state == "#":
|
elif state == "#":
|
||||||
dirs = [dirs[3]] + dirs[:3]
|
dirs = [dirs[3]] + dirs[:3]
|
||||||
elif state == 'F':
|
elif state == "F":
|
||||||
dirs = dirs[2:] + dirs[:2]
|
dirs = dirs[2:] + dirs[:2]
|
||||||
return dirs
|
return dirs
|
||||||
|
|
||||||
|
|
@ -61,16 +61,16 @@ class Solution(BaseSolution):
|
||||||
i, p = existing
|
i, p = existing
|
||||||
return amap, p[2]
|
return amap, p[2]
|
||||||
else:
|
else:
|
||||||
amap.append([pos[0], pos[1], '.'])
|
amap.append([pos[0], pos[1], "."])
|
||||||
return amap, '.'
|
return amap, "."
|
||||||
|
|
||||||
def _update_state(self, amap, pos):
|
def _update_state(self, amap, pos):
|
||||||
t = lambda x: x[1][0] == pos[0] and x[1][1] == pos[1]
|
t = lambda x: x[1][0] == pos[0] and x[1][1] == pos[1]
|
||||||
existing = next(filter(t, enumerate(amap)))
|
existing = next(filter(t, enumerate(amap)))
|
||||||
i, p = existing
|
i, p = existing
|
||||||
if p[2] == '.':
|
if p[2] == ".":
|
||||||
self.infected += 1
|
self.infected += 1
|
||||||
amap[i][2] = '.' if p[2] == '#' else '#'
|
amap[i][2] = "." if p[2] == "#" else "#"
|
||||||
return amap
|
return amap
|
||||||
|
|
||||||
def _move(self, pos, d):
|
def _move(self, pos, d):
|
||||||
|
|
@ -80,12 +80,12 @@ class Solution(BaseSolution):
|
||||||
t = lambda x: x[1][0] == pos[0] and x[1][1] == pos[1]
|
t = lambda x: x[1][0] == pos[0] and x[1][1] == pos[1]
|
||||||
existing = next(filter(t, enumerate(amap)))
|
existing = next(filter(t, enumerate(amap)))
|
||||||
i, p = existing
|
i, p = existing
|
||||||
if p[2] == '.':
|
if p[2] == ".":
|
||||||
ns = 'W'
|
ns = "W"
|
||||||
elif p[2] == 'W':
|
elif p[2] == "W":
|
||||||
self.infected += 1
|
self.infected += 1
|
||||||
ns = "#"
|
ns = "#"
|
||||||
elif p[2] == '#':
|
elif p[2] == "#":
|
||||||
ns = "F"
|
ns = "F"
|
||||||
else:
|
else:
|
||||||
ns = "."
|
ns = "."
|
||||||
|
|
@ -93,6 +93,6 @@ class Solution(BaseSolution):
|
||||||
return amap
|
return amap
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
solution = Solution()
|
solution = Solution()
|
||||||
solution.show_results()
|
solution.show_results()
|
||||||
|
|
|
||||||
138
2017-python/solutions/day_23.py
Normal file
138
2017-python/solutions/day_23.py
Normal file
|
|
@ -0,0 +1,138 @@
|
||||||
|
from solutions import BaseSolution
|
||||||
|
from collections import defaultdict
|
||||||
|
|
||||||
|
|
||||||
|
class Solution(BaseSolution):
|
||||||
|
input_file = "23.txt"
|
||||||
|
sound_freq = 0
|
||||||
|
queue = [], []
|
||||||
|
sent = [0, 0]
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return "Day 23: Coprocessor Conflagration"
|
||||||
|
|
||||||
|
def solve(self, puzzle_input):
|
||||||
|
R = defaultdict(int)
|
||||||
|
P = puzzle_input.splitlines()
|
||||||
|
i = 0
|
||||||
|
M = 0
|
||||||
|
while i < len(P):
|
||||||
|
j = 1
|
||||||
|
w, r, v = P[i].split()
|
||||||
|
match w:
|
||||||
|
case "set":
|
||||||
|
R[r] = R[v] if v in "abcdefgh" else int(v)
|
||||||
|
case "sub":
|
||||||
|
R[r] -= R[v] if v in "abcdefgh" else int(v)
|
||||||
|
case "mul":
|
||||||
|
M += 1
|
||||||
|
R[r] *= R[v] if v in "abcdefgh" else int(v)
|
||||||
|
case "jnz":
|
||||||
|
x = R[r] if r in "abcdefgh" else int(r)
|
||||||
|
if x != 0:
|
||||||
|
j = R[v] if v in "abcdefgh" else int(v)
|
||||||
|
i += j
|
||||||
|
return M
|
||||||
|
|
||||||
|
def solve_again(self, puzzle_input):
|
||||||
|
def isprime(num):
|
||||||
|
for n in range(2, int(num**0.5) + 1):
|
||||||
|
if num % n == 0:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
b = 99 * 100 + 100_000
|
||||||
|
c = b + 17_000 + 1
|
||||||
|
|
||||||
|
return sum(not isprime(n) for n in range(b, c, 17))
|
||||||
|
|
||||||
|
def python_reconstruction(self, puzzle_input):
|
||||||
|
R = defaultdict(int)
|
||||||
|
R["a"] = 1
|
||||||
|
|
||||||
|
# 00: set b 99
|
||||||
|
R["b"] = 99
|
||||||
|
# 01: set c b
|
||||||
|
R["c"] = R["b"]
|
||||||
|
|
||||||
|
# 02: jnz a 2
|
||||||
|
if R["a"] == 0:
|
||||||
|
# 03: jnz 1 5
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
# 04: mul b 100
|
||||||
|
# 05: sub b -100000
|
||||||
|
R["b"] = R["b"] * 100 + 100_000
|
||||||
|
|
||||||
|
# 06: set c b
|
||||||
|
# 07: sub c -17000
|
||||||
|
R["c"] = R["b"] + 17_000
|
||||||
|
|
||||||
|
while True:
|
||||||
|
# 08: set f 1
|
||||||
|
# 09: set d 2
|
||||||
|
R["f"] = 1
|
||||||
|
R["d"] = 2
|
||||||
|
|
||||||
|
while True:
|
||||||
|
# 10: set e 2
|
||||||
|
R["e"] = 2
|
||||||
|
|
||||||
|
while True:
|
||||||
|
# 11: set g d
|
||||||
|
# 12: mul g e
|
||||||
|
# 13: sub g b
|
||||||
|
R["g"] = R["d"] * R["e"] - R["b"]
|
||||||
|
|
||||||
|
# 14: jnz g 2
|
||||||
|
if R["g"] == 0:
|
||||||
|
# 15: set f 0
|
||||||
|
R["f"] = 0
|
||||||
|
|
||||||
|
# 16: sub e -1
|
||||||
|
R["e"] += 1
|
||||||
|
|
||||||
|
# 17: set g e
|
||||||
|
# 18: sub g b
|
||||||
|
R["g"] = R["e"] - R["b"]
|
||||||
|
|
||||||
|
# 19: jnz g -8
|
||||||
|
if R["g"] == 0:
|
||||||
|
break
|
||||||
|
|
||||||
|
# 20: sub d -1
|
||||||
|
R["d"] += 1
|
||||||
|
|
||||||
|
# 21: set g d
|
||||||
|
# 22: sub g b
|
||||||
|
R["g"] = R["d"] - R["b"]
|
||||||
|
|
||||||
|
# 23: jnz g -13
|
||||||
|
if R["g"] == 0:
|
||||||
|
break
|
||||||
|
|
||||||
|
# 24: jnz f 2
|
||||||
|
if R["f"] == 0:
|
||||||
|
# 25: sub h -1
|
||||||
|
R["h"] += 1
|
||||||
|
|
||||||
|
# 26: set g b
|
||||||
|
# 27: sub g c
|
||||||
|
R["g"] = R["b"] - R["c"]
|
||||||
|
|
||||||
|
# 28: jnz g 2
|
||||||
|
if R["g"] == 0:
|
||||||
|
# 29: jnz 1 3
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
# 30: sub b -17
|
||||||
|
R["b"] += 17
|
||||||
|
|
||||||
|
# 31: jnz 1 -23
|
||||||
|
|
||||||
|
return R["h"]
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
solution = Solution()
|
||||||
|
solution.show_results()
|
||||||
51
2017-python/solutions/day_24.py
Normal file
51
2017-python/solutions/day_24.py
Normal file
|
|
@ -0,0 +1,51 @@
|
||||||
|
from solutions import BaseSolution
|
||||||
|
import re
|
||||||
|
|
||||||
|
|
||||||
|
class Solution(BaseSolution):
|
||||||
|
input_file = "24.txt"
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return "Day 24: Electromagnetic Moat"
|
||||||
|
|
||||||
|
def solve(self, puzzle_input):
|
||||||
|
p1, _ = self._solve(puzzle_input)
|
||||||
|
return p1
|
||||||
|
|
||||||
|
def solve_again(self, puzzle_input):
|
||||||
|
_, p2 = self._solve(puzzle_input)
|
||||||
|
return p2
|
||||||
|
|
||||||
|
def _solve(self, puzzle_input):
|
||||||
|
components = [
|
||||||
|
tuple(map(int, re.findall(r"\d+", line))) for line in puzzle_input.split()
|
||||||
|
]
|
||||||
|
|
||||||
|
Q = [(c, []) for c in components if 0 in c]
|
||||||
|
S = 0
|
||||||
|
L = 0
|
||||||
|
LS = 0
|
||||||
|
|
||||||
|
while Q:
|
||||||
|
c, s = Q.pop()
|
||||||
|
if c in s:
|
||||||
|
S = max(S, sum(x + y for x, y in s))
|
||||||
|
if len(s) >= L:
|
||||||
|
L = len(s)
|
||||||
|
LS = max(LS, L * 1_000_000 + sum(x + y for x, y in s))
|
||||||
|
continue
|
||||||
|
cc = set(c) if not s else set(c) - set(s[-1])
|
||||||
|
if not cc:
|
||||||
|
cc = set(c)
|
||||||
|
for p1, p2 in components:
|
||||||
|
if p1 > 0 and p1 in cc:
|
||||||
|
Q.append(((p1, p2), s + [c]))
|
||||||
|
if p2 > 0 and p2 in cc:
|
||||||
|
Q.append(((p1, p2), s + [c]))
|
||||||
|
|
||||||
|
return S, LS % 1_000_000
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
solution = Solution()
|
||||||
|
solution.show_results()
|
||||||
46
2017-python/solutions/day_25.py
Normal file
46
2017-python/solutions/day_25.py
Normal file
|
|
@ -0,0 +1,46 @@
|
||||||
|
from solutions import BaseSolution
|
||||||
|
from collections import defaultdict
|
||||||
|
import re
|
||||||
|
|
||||||
|
|
||||||
|
class Solution(BaseSolution):
|
||||||
|
input_file = "25.txt"
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return "Day 25: The Halting Problem"
|
||||||
|
|
||||||
|
def solve(self, puzzle_input):
|
||||||
|
T = defaultdict(int)
|
||||||
|
p = 0
|
||||||
|
|
||||||
|
hd, *states = puzzle_input.split("\n\n")
|
||||||
|
S = int(re.findall(r"(\d+) steps", hd)[0])
|
||||||
|
s = re.findall(r"state (\w)\.", hd)[0]
|
||||||
|
P = {}
|
||||||
|
for state in states:
|
||||||
|
k = re.findall(r"state (\w)\:", state)[0]
|
||||||
|
v = [int(n) for n in re.findall(r"value (\d)\.", state)]
|
||||||
|
d = re.findall(r"(left|right)\.", state)
|
||||||
|
t = re.findall(r"state (\w)\.", state)
|
||||||
|
P[k] = (v[0], d[0], t[0], v[1], d[1], t[1])
|
||||||
|
|
||||||
|
for _ in range(S):
|
||||||
|
v0, d0, s0, v1, d1, s1 = P[s]
|
||||||
|
match T[p]:
|
||||||
|
case 0:
|
||||||
|
T[p] = v0
|
||||||
|
s = s0
|
||||||
|
p += 1 if d0 == "right" else -1
|
||||||
|
case 1:
|
||||||
|
T[p] = v1
|
||||||
|
s = s1
|
||||||
|
p += 1 if d1 == "right" else -1
|
||||||
|
return sum(T.values())
|
||||||
|
|
||||||
|
def solve_again(self, puzzle_input):
|
||||||
|
return "*"
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
solution = Solution()
|
||||||
|
solution.show_results()
|
||||||
|
|
@ -8,18 +8,18 @@ class Day1TestCase(unittest.TestCase):
|
||||||
self.solution = Solution()
|
self.solution = Solution()
|
||||||
|
|
||||||
def test_sums_equal_pairs(self):
|
def test_sums_equal_pairs(self):
|
||||||
assert self.solution.solve('1122') == 3
|
assert self.solution.solve("1122") == 3
|
||||||
assert self.solution.solve('1111') == 4
|
assert self.solution.solve("1111") == 4
|
||||||
assert self.solution.solve('1234') == 0
|
assert self.solution.solve("1234") == 0
|
||||||
assert self.solution.solve('91212129') == 9
|
assert self.solution.solve("91212129") == 9
|
||||||
|
|
||||||
def test_sums_equal_pairs_halvway_around(self):
|
def test_sums_equal_pairs_halvway_around(self):
|
||||||
assert self.solution.solve_again('1212') == 6
|
assert self.solution.solve_again("1212") == 6
|
||||||
assert self.solution.solve_again('1221') == 0
|
assert self.solution.solve_again("1221") == 0
|
||||||
assert self.solution.solve_again('123425') == 4
|
assert self.solution.solve_again("123425") == 4
|
||||||
assert self.solution.solve_again('123123') == 12
|
assert self.solution.solve_again("123123") == 12
|
||||||
assert self.solution.solve_again('12131415') == 4
|
assert self.solution.solve_again("12131415") == 4
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
||||||
|
|
@ -13,19 +13,19 @@ class Day2TestCase(unittest.TestCase):
|
||||||
assert self.solution.get_diff([2, 4, 6, 8]) == 6
|
assert self.solution.get_diff([2, 4, 6, 8]) == 6
|
||||||
|
|
||||||
def test_calculates_checksum(self):
|
def test_calculates_checksum(self):
|
||||||
puzzle_input = '\n'.join(['5 1 9 5', '7 5 3', '2 4 6 8'])
|
puzzle_input = "\n".join(["5 1 9 5", "7 5 3", "2 4 6 8"])
|
||||||
assert self.solution.solve(puzzle_input) == 18
|
assert self.solution.solve(puzzle_input) == 18
|
||||||
|
|
||||||
def test_calculates_row_even_divisible(self):
|
def test_calculates_row_even_divisible(self):
|
||||||
puzzle_input = '\n'.join(['5 9 2 8', '9 4 7 3', '3 8 6 5'])
|
puzzle_input = "\n".join(["5 9 2 8", "9 4 7 3", "3 8 6 5"])
|
||||||
assert self.solution.get_even_divisible([5, 9, 2, 8]) == 4
|
assert self.solution.get_even_divisible([5, 9, 2, 8]) == 4
|
||||||
assert self.solution.get_even_divisible([9, 4, 7, 3]) == 3
|
assert self.solution.get_even_divisible([9, 4, 7, 3]) == 3
|
||||||
assert self.solution.get_even_divisible([3, 8, 6, 5]) == 2
|
assert self.solution.get_even_divisible([3, 8, 6, 5]) == 2
|
||||||
|
|
||||||
def test_calculates_row_result_sum(self):
|
def test_calculates_row_result_sum(self):
|
||||||
puzzle_input = '\n'.join(['5 9 2 8', '9 4 7 3', '3 8 6 5'])
|
puzzle_input = "\n".join(["5 9 2 8", "9 4 7 3", "3 8 6 5"])
|
||||||
assert self.solution.solve_again(puzzle_input) == 9
|
assert self.solution.solve_again(puzzle_input) == 9
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
||||||
|
|
@ -8,11 +8,11 @@ class Day3TestCase(unittest.TestCase):
|
||||||
self.solution = Solution()
|
self.solution = Solution()
|
||||||
|
|
||||||
def test_shortest_manhattan_distance(self):
|
def test_shortest_manhattan_distance(self):
|
||||||
assert self.solution.solve('1') == 0
|
assert self.solution.solve("1") == 0
|
||||||
assert self.solution.solve('12') == 3
|
assert self.solution.solve("12") == 3
|
||||||
assert self.solution.solve('23') == 2
|
assert self.solution.solve("23") == 2
|
||||||
assert self.solution.solve('1024') == 31
|
assert self.solution.solve("1024") == 31
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
||||||
|
|
@ -9,22 +9,22 @@ class Day4TestCase(unittest.TestCase):
|
||||||
|
|
||||||
def test_passphrase_has_only_unique_words(self):
|
def test_passphrase_has_only_unique_words(self):
|
||||||
passphrases = [
|
passphrases = [
|
||||||
'aa bb cc dd ee',
|
"aa bb cc dd ee",
|
||||||
'aa bb cc dd aa',
|
"aa bb cc dd aa",
|
||||||
'aa bb cc dd aaa',
|
"aa bb cc dd aaa",
|
||||||
]
|
]
|
||||||
assert self.solution.validate(passphrases[0]) == True
|
assert self.solution.validate(passphrases[0]) == True
|
||||||
assert self.solution.validate(passphrases[1]) == False
|
assert self.solution.validate(passphrases[1]) == False
|
||||||
assert self.solution.validate(passphrases[2]) == True
|
assert self.solution.validate(passphrases[2]) == True
|
||||||
assert self.solution.solve('\n'.join(passphrases)) == 2
|
assert self.solution.solve("\n".join(passphrases)) == 2
|
||||||
|
|
||||||
def test_passphrase_has_no_anagrams(self):
|
def test_passphrase_has_no_anagrams(self):
|
||||||
passphrases = [
|
passphrases = [
|
||||||
'abcde fghij',
|
"abcde fghij",
|
||||||
'abcde xyz ecdab',
|
"abcde xyz ecdab",
|
||||||
'a ab abc abd abf abj',
|
"a ab abc abd abf abj",
|
||||||
'iiii oiii ooii oooi oooo',
|
"iiii oiii ooii oooi oooo",
|
||||||
'oiii ioii iioi iiio',
|
"oiii ioii iioi iiio",
|
||||||
]
|
]
|
||||||
assert self.solution.validate(passphrases[0], True) == True
|
assert self.solution.validate(passphrases[0], True) == True
|
||||||
assert self.solution.validate(passphrases[1], True) == False
|
assert self.solution.validate(passphrases[1], True) == False
|
||||||
|
|
@ -33,5 +33,5 @@ class Day4TestCase(unittest.TestCase):
|
||||||
assert self.solution.validate(passphrases[4], True) == False
|
assert self.solution.validate(passphrases[4], True) == False
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
||||||
|
|
@ -8,13 +8,29 @@ class Day5TestCase(unittest.TestCase):
|
||||||
self.solution = Solution()
|
self.solution = Solution()
|
||||||
|
|
||||||
def test_calculate_exit_distance(self):
|
def test_calculate_exit_distance(self):
|
||||||
puzzle_input = '\n'.join(['0', '3', '0', '1', '-3',])
|
puzzle_input = "\n".join(
|
||||||
|
[
|
||||||
|
"0",
|
||||||
|
"3",
|
||||||
|
"0",
|
||||||
|
"1",
|
||||||
|
"-3",
|
||||||
|
]
|
||||||
|
)
|
||||||
assert self.solution.solve(puzzle_input) == 5
|
assert self.solution.solve(puzzle_input) == 5
|
||||||
|
|
||||||
def test_calculate_stranger_exit_distance(self):
|
def test_calculate_stranger_exit_distance(self):
|
||||||
puzzle_input = '\n'.join(['0', '3', '0', '1', '-3',])
|
puzzle_input = "\n".join(
|
||||||
|
[
|
||||||
|
"0",
|
||||||
|
"3",
|
||||||
|
"0",
|
||||||
|
"1",
|
||||||
|
"-3",
|
||||||
|
]
|
||||||
|
)
|
||||||
assert self.solution.solve_again(puzzle_input) == 10
|
assert self.solution.solve_again(puzzle_input) == 10
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ class Day6TestCase(unittest.TestCase):
|
||||||
self.solution = Solution()
|
self.solution = Solution()
|
||||||
|
|
||||||
def test_count_redistribution_cycles(self):
|
def test_count_redistribution_cycles(self):
|
||||||
puzzle_input = '0 2 7 0'
|
puzzle_input = "0 2 7 0"
|
||||||
banks = list(map(int, puzzle_input.split()))
|
banks = list(map(int, puzzle_input.split()))
|
||||||
assert self.solution.redistribute(banks) == (2, 4, 1, 2)
|
assert self.solution.redistribute(banks) == (2, 4, 1, 2)
|
||||||
assert self.solution.redistribute((2, 4, 1, 2)) == (3, 1, 2, 3)
|
assert self.solution.redistribute((2, 4, 1, 2)) == (3, 1, 2, 3)
|
||||||
|
|
@ -18,9 +18,9 @@ class Day6TestCase(unittest.TestCase):
|
||||||
assert self.solution.solve(puzzle_input) == 5
|
assert self.solution.solve(puzzle_input) == 5
|
||||||
|
|
||||||
def test_count_redistribution_cycles_again(self):
|
def test_count_redistribution_cycles_again(self):
|
||||||
puzzle_input = '0 2 7 0'
|
puzzle_input = "0 2 7 0"
|
||||||
assert self.solution.solve_again(puzzle_input) == 4
|
assert self.solution.solve_again(puzzle_input) == 4
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ from solutions.day_07 import Solution, Program
|
||||||
|
|
||||||
class Day7TestCase(unittest.TestCase):
|
class Day7TestCase(unittest.TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.puzzle_input = '''
|
self.puzzle_input = """
|
||||||
pbga (66)
|
pbga (66)
|
||||||
xhth (57)
|
xhth (57)
|
||||||
ebii (61)
|
ebii (61)
|
||||||
|
|
@ -19,24 +19,24 @@ class Day7TestCase(unittest.TestCase):
|
||||||
ugml (68) -> gyxo, ebii, jptl
|
ugml (68) -> gyxo, ebii, jptl
|
||||||
gyxo (61)
|
gyxo (61)
|
||||||
cntj (57)
|
cntj (57)
|
||||||
'''.strip()
|
""".strip()
|
||||||
self.solution = Solution()
|
self.solution = Solution()
|
||||||
|
|
||||||
def test_find_bottom_tower(self):
|
def test_find_bottom_tower(self):
|
||||||
p = Program('ugml (68) -> gyxo, ebii, jptl')
|
p = Program("ugml (68) -> gyxo, ebii, jptl")
|
||||||
assert p.name == 'ugml'
|
assert p.name == "ugml"
|
||||||
assert p.weight == 68
|
assert p.weight == 68
|
||||||
assert p.disc == ('gyxo', 'ebii', 'jptl')
|
assert p.disc == ("gyxo", "ebii", "jptl")
|
||||||
p = Program('jptl (61)')
|
p = Program("jptl (61)")
|
||||||
assert p.name == 'jptl'
|
assert p.name == "jptl"
|
||||||
assert p.weight == 61
|
assert p.weight == 61
|
||||||
assert p.disc == ()
|
assert p.disc == ()
|
||||||
assert self.solution.solve(self.puzzle_input).name == 'tknk'
|
assert self.solution.solve(self.puzzle_input).name == "tknk"
|
||||||
|
|
||||||
def test_find_weight_correction(self):
|
def test_find_weight_correction(self):
|
||||||
corrected = self.solution.solve_again(self.puzzle_input)
|
corrected = self.solution.solve_again(self.puzzle_input)
|
||||||
assert corrected == 60
|
assert corrected == 60
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
||||||
|
|
@ -8,23 +8,23 @@ class Day8TestCase(unittest.TestCase):
|
||||||
self.solution = Solution()
|
self.solution = Solution()
|
||||||
|
|
||||||
def test_largest_registry_value(self):
|
def test_largest_registry_value(self):
|
||||||
puzzle_input = '''
|
puzzle_input = """
|
||||||
b inc 5 if a > 1
|
b inc 5 if a > 1
|
||||||
a inc 1 if b < 5
|
a inc 1 if b < 5
|
||||||
c dec -10 if a >= 1
|
c dec -10 if a >= 1
|
||||||
c inc -20 if c == 10
|
c inc -20 if c == 10
|
||||||
'''.strip()
|
""".strip()
|
||||||
assert self.solution.solve(puzzle_input) == 1
|
assert self.solution.solve(puzzle_input) == 1
|
||||||
|
|
||||||
def test_largest_ath_registry_value(self):
|
def test_largest_ath_registry_value(self):
|
||||||
puzzle_input = '''
|
puzzle_input = """
|
||||||
b inc 5 if a > 1
|
b inc 5 if a > 1
|
||||||
a inc 1 if b < 5
|
a inc 1 if b < 5
|
||||||
c dec -10 if a >= 1
|
c dec -10 if a >= 1
|
||||||
c inc -20 if c == 10
|
c inc -20 if c == 10
|
||||||
'''.strip()
|
""".strip()
|
||||||
assert self.solution.solve_again(puzzle_input) == 10
|
assert self.solution.solve_again(puzzle_input) == 10
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
||||||
|
|
@ -8,24 +8,24 @@ class Day9TestCase(unittest.TestCase):
|
||||||
self.solution = Solution()
|
self.solution = Solution()
|
||||||
|
|
||||||
def test_calculates_score(self):
|
def test_calculates_score(self):
|
||||||
assert self.solution.solve('{}') == 1
|
assert self.solution.solve("{}") == 1
|
||||||
assert self.solution.solve('{{{}}}') == 6
|
assert self.solution.solve("{{{}}}") == 6
|
||||||
assert self.solution.solve('{{},{}}') == 5
|
assert self.solution.solve("{{},{}}") == 5
|
||||||
assert self.solution.solve('{{{},{},{{}}}}') == 16
|
assert self.solution.solve("{{{},{},{{}}}}") == 16
|
||||||
assert self.solution.solve('{<a>,<a>,<a>,<a>}') == 1
|
assert self.solution.solve("{<a>,<a>,<a>,<a>}") == 1
|
||||||
assert self.solution.solve('{{<ab>},{<ab>},{<ab>},{<ab>}}') == 9
|
assert self.solution.solve("{{<ab>},{<ab>},{<ab>},{<ab>}}") == 9
|
||||||
assert self.solution.solve('{{<!!>},{<!!>},{<!!>},{<!!>}}') == 9
|
assert self.solution.solve("{{<!!>},{<!!>},{<!!>},{<!!>}}") == 9
|
||||||
assert self.solution.solve('{{<a!>},{<a!>},{<a!>},{<ab>}}') == 3
|
assert self.solution.solve("{{<a!>},{<a!>},{<a!>},{<ab>}}") == 3
|
||||||
|
|
||||||
def test_count_garbage(self):
|
def test_count_garbage(self):
|
||||||
assert self.solution.solve_again('<>') == 0
|
assert self.solution.solve_again("<>") == 0
|
||||||
assert self.solution.solve_again('<random characters>') == 17
|
assert self.solution.solve_again("<random characters>") == 17
|
||||||
assert self.solution.solve_again('<<<<>') == 3
|
assert self.solution.solve_again("<<<<>") == 3
|
||||||
assert self.solution.solve_again('<{!>}>') == 2
|
assert self.solution.solve_again("<{!>}>") == 2
|
||||||
assert self.solution.solve_again('<!!>') == 0
|
assert self.solution.solve_again("<!!>") == 0
|
||||||
assert self.solution.solve_again('<!!!>>') == 0
|
assert self.solution.solve_again("<!!!>>") == 0
|
||||||
assert self.solution.solve_again('<{o"i!a,<{i<a>') == 10
|
assert self.solution.solve_again('<{o"i!a,<{i<a>') == 10
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
||||||
|
|
@ -25,14 +25,16 @@ class Day10TestCase(unittest.TestCase):
|
||||||
assert self.solution.list == [3, 4, 2, 1, 0]
|
assert self.solution.list == [3, 4, 2, 1, 0]
|
||||||
assert self.solution.skip_size == 4
|
assert self.solution.skip_size == 4
|
||||||
assert self.solution.pos == 4
|
assert self.solution.pos == 4
|
||||||
assert self.solution.solve('3,4,1,5', r=5) == 12
|
assert self.solution.solve("3,4,1,5", r=5) == 12
|
||||||
|
|
||||||
def test_dense_hash(self):
|
def test_dense_hash(self):
|
||||||
assert self.solution.solve_again('') == 'a2582a3a0e66e6e86e3812dcb672a272'
|
assert self.solution.solve_again("") == "a2582a3a0e66e6e86e3812dcb672a272"
|
||||||
assert self.solution.solve_again('AoC 2017') == '33efeb34ea91902bb2f59c9920caa6cd'
|
assert (
|
||||||
assert self.solution.solve_again('1,2,3') == '3efbe78a8d82f29979031a4aa0b16a9d'
|
self.solution.solve_again("AoC 2017") == "33efeb34ea91902bb2f59c9920caa6cd"
|
||||||
assert self.solution.solve_again('1,2,4') == '63960835bcdc130f0b66d7ff4f6a5a8e'
|
)
|
||||||
|
assert self.solution.solve_again("1,2,3") == "3efbe78a8d82f29979031a4aa0b16a9d"
|
||||||
|
assert self.solution.solve_again("1,2,4") == "63960835bcdc130f0b66d7ff4f6a5a8e"
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
||||||
|
|
@ -8,15 +8,15 @@ class Day11TestCase(unittest.TestCase):
|
||||||
self.solution = Solution()
|
self.solution = Solution()
|
||||||
|
|
||||||
def test_distance(self):
|
def test_distance(self):
|
||||||
assert self.solution.solve('ne,ne,ne') == 3
|
assert self.solution.solve("ne,ne,ne") == 3
|
||||||
assert self.solution.solve('ne,ne,sw,sw') == 0
|
assert self.solution.solve("ne,ne,sw,sw") == 0
|
||||||
assert self.solution.solve('ne,ne,s,s') == 2
|
assert self.solution.solve("ne,ne,s,s") == 2
|
||||||
assert self.solution.solve('se,sw,se,sw,sw') == 3
|
assert self.solution.solve("se,sw,se,sw,sw") == 3
|
||||||
|
|
||||||
def test_furthest_away(self):
|
def test_furthest_away(self):
|
||||||
assert self.solution.solve_again('ne,ne,sw,sw') == 2
|
assert self.solution.solve_again("ne,ne,sw,sw") == 2
|
||||||
assert self.solution.solve_again('se,sw,se,sw,sw') == 3
|
assert self.solution.solve_again("se,sw,se,sw,sw") == 3
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ class Day12TestCase(unittest.TestCase):
|
||||||
self.solution = Solution()
|
self.solution = Solution()
|
||||||
|
|
||||||
def test_connected_to_program0(self):
|
def test_connected_to_program0(self):
|
||||||
puzzle_input = '''
|
puzzle_input = """
|
||||||
0 <-> 2
|
0 <-> 2
|
||||||
1 <-> 1
|
1 <-> 1
|
||||||
2 <-> 0, 3, 4
|
2 <-> 0, 3, 4
|
||||||
|
|
@ -16,11 +16,11 @@ class Day12TestCase(unittest.TestCase):
|
||||||
4 <-> 2, 3, 6
|
4 <-> 2, 3, 6
|
||||||
5 <-> 6
|
5 <-> 6
|
||||||
6 <-> 4, 5
|
6 <-> 4, 5
|
||||||
'''.strip()
|
""".strip()
|
||||||
assert self.solution.solve(puzzle_input) == 6
|
assert self.solution.solve(puzzle_input) == 6
|
||||||
|
|
||||||
def test_group_coun(self):
|
def test_group_coun(self):
|
||||||
puzzle_input = '''
|
puzzle_input = """
|
||||||
0 <-> 2
|
0 <-> 2
|
||||||
1 <-> 1
|
1 <-> 1
|
||||||
2 <-> 0, 3, 4
|
2 <-> 0, 3, 4
|
||||||
|
|
@ -28,9 +28,9 @@ class Day12TestCase(unittest.TestCase):
|
||||||
4 <-> 2, 3, 6
|
4 <-> 2, 3, 6
|
||||||
5 <-> 6
|
5 <-> 6
|
||||||
6 <-> 4, 5
|
6 <-> 4, 5
|
||||||
'''.strip()
|
""".strip()
|
||||||
assert self.solution.solve_again(puzzle_input) == 2
|
assert self.solution.solve_again(puzzle_input) == 2
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
||||||
|
|
@ -8,23 +8,23 @@ class Day13TestCase(unittest.TestCase):
|
||||||
self.solution = Solution()
|
self.solution = Solution()
|
||||||
|
|
||||||
def test_get_through_firewall(self):
|
def test_get_through_firewall(self):
|
||||||
puzzle_input = '''
|
puzzle_input = """
|
||||||
0: 3
|
0: 3
|
||||||
1: 2
|
1: 2
|
||||||
4: 4
|
4: 4
|
||||||
6: 4
|
6: 4
|
||||||
'''.strip()
|
""".strip()
|
||||||
assert self.solution.solve(puzzle_input) == 24
|
assert self.solution.solve(puzzle_input) == 24
|
||||||
|
|
||||||
def test_wait(self):
|
def test_wait(self):
|
||||||
puzzle_input = '''
|
puzzle_input = """
|
||||||
0: 3
|
0: 3
|
||||||
1: 2
|
1: 2
|
||||||
4: 4
|
4: 4
|
||||||
6: 4
|
6: 4
|
||||||
'''.strip()
|
""".strip()
|
||||||
assert self.solution.solve_again(puzzle_input) == 10
|
assert self.solution.solve_again(puzzle_input) == 10
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
||||||
|
|
@ -8,11 +8,11 @@ class Day14TestCase(unittest.TestCase):
|
||||||
self.solution = Solution()
|
self.solution = Solution()
|
||||||
|
|
||||||
def test_used_squares(self):
|
def test_used_squares(self):
|
||||||
assert self.solution.solve('flqrgnkx') == 8108
|
assert self.solution.solve("flqrgnkx") == 8108
|
||||||
|
|
||||||
def test_regions(self):
|
def test_regions(self):
|
||||||
assert self.solution.solve_again('flqrgnkx') == 1242
|
assert self.solution.solve_again("flqrgnkx") == 1242
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
||||||
|
|
@ -8,19 +8,19 @@ class Day15TestCase(unittest.TestCase):
|
||||||
self.solution = Solution()
|
self.solution = Solution()
|
||||||
|
|
||||||
def test_40m_pairs(self):
|
def test_40m_pairs(self):
|
||||||
puzzle_input = '''
|
puzzle_input = """
|
||||||
Generator A starts with 65
|
Generator A starts with 65
|
||||||
Generator B starts with 8921
|
Generator B starts with 8921
|
||||||
'''.strip()
|
""".strip()
|
||||||
# assert self.solution.solve(puzzle_input) == 588
|
# assert self.solution.solve(puzzle_input) == 588
|
||||||
|
|
||||||
def test_5k_pairs(self):
|
def test_5k_pairs(self):
|
||||||
puzzle_input = '''
|
puzzle_input = """
|
||||||
Generator A starts with 65
|
Generator A starts with 65
|
||||||
Generator B starts with 8921
|
Generator B starts with 8921
|
||||||
'''.strip()
|
""".strip()
|
||||||
assert self.solution.solve_again(puzzle_input) == 309
|
assert self.solution.solve_again(puzzle_input) == 309
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
||||||
|
|
@ -8,9 +8,9 @@ class Day16TestCase(unittest.TestCase):
|
||||||
self.solution = Solution()
|
self.solution = Solution()
|
||||||
|
|
||||||
def test_something(self):
|
def test_something(self):
|
||||||
puzzle_input = '''s1,x3/4,pe/b'''.strip()
|
puzzle_input = """s1,x3/4,pe/b""".strip()
|
||||||
assert self.solution.solve(puzzle_input, 5) == 'baedc'
|
assert self.solution.solve(puzzle_input, 5) == "baedc"
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
||||||
|
|
@ -8,8 +8,8 @@ class Day17TestCase(unittest.TestCase):
|
||||||
self.solution = Solution()
|
self.solution = Solution()
|
||||||
|
|
||||||
def test_something(self):
|
def test_something(self):
|
||||||
assert self.solution.solve('3') == 638
|
assert self.solution.solve("3") == 638
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ class Day18TestCase(unittest.TestCase):
|
||||||
self.solution = Solution()
|
self.solution = Solution()
|
||||||
|
|
||||||
def test_something(self):
|
def test_something(self):
|
||||||
puzzle_input = '''
|
puzzle_input = """
|
||||||
set a 1
|
set a 1
|
||||||
add a 2
|
add a 2
|
||||||
mul a a
|
mul a a
|
||||||
|
|
@ -19,11 +19,11 @@ class Day18TestCase(unittest.TestCase):
|
||||||
jgz a -1
|
jgz a -1
|
||||||
set a 1
|
set a 1
|
||||||
jgz a -2
|
jgz a -2
|
||||||
'''.strip()
|
""".strip()
|
||||||
assert self.solution.solve(puzzle_input) == 4
|
assert self.solution.solve(puzzle_input) == 4
|
||||||
|
|
||||||
def test_something_else(self):
|
def test_something_else(self):
|
||||||
puzzle_input = '''
|
puzzle_input = """
|
||||||
snd 1
|
snd 1
|
||||||
snd 2
|
snd 2
|
||||||
snd p
|
snd p
|
||||||
|
|
@ -31,9 +31,9 @@ class Day18TestCase(unittest.TestCase):
|
||||||
rcv b
|
rcv b
|
||||||
rcv c
|
rcv c
|
||||||
rcv d
|
rcv d
|
||||||
'''.strip()
|
""".strip()
|
||||||
assert self.solution.solve_again(puzzle_input) == 3
|
assert self.solution.solve_again(puzzle_input) == 3
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
||||||
|
|
@ -8,27 +8,27 @@ class Day19TestCase(unittest.TestCase):
|
||||||
self.solution = Solution()
|
self.solution = Solution()
|
||||||
|
|
||||||
def test_seen(self):
|
def test_seen(self):
|
||||||
puzzle_input = ''' |
|
puzzle_input = """ |
|
||||||
| +--+
|
| +--+
|
||||||
A | C
|
A | C
|
||||||
F---|----E|--+
|
F---|----E|--+
|
||||||
| | | D
|
| | | D
|
||||||
+B-+ +--+
|
+B-+ +--+
|
||||||
|
|
||||||
'''
|
"""
|
||||||
assert self.solution.solve(puzzle_input) == 'ABCDEF'
|
assert self.solution.solve(puzzle_input) == "ABCDEF"
|
||||||
|
|
||||||
def test_steps(self):
|
def test_steps(self):
|
||||||
puzzle_input = ''' |
|
puzzle_input = """ |
|
||||||
| +--+
|
| +--+
|
||||||
A | C
|
A | C
|
||||||
F---|----E|--+
|
F---|----E|--+
|
||||||
| | | D
|
| | | D
|
||||||
+B-+ +--+
|
+B-+ +--+
|
||||||
|
|
||||||
'''
|
"""
|
||||||
assert self.solution.solve_again(puzzle_input) == 38
|
assert self.solution.solve_again(puzzle_input) == 38
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
||||||
|
|
@ -8,21 +8,21 @@ class Day20TestCase(unittest.TestCase):
|
||||||
self.solution = Solution()
|
self.solution = Solution()
|
||||||
|
|
||||||
def test_shortest_distance_over_time(self):
|
def test_shortest_distance_over_time(self):
|
||||||
puzzle_input = '''
|
puzzle_input = """
|
||||||
p=< 3,0,0>, v=< 2,0,0>, a=<-1,0,0>
|
p=< 3,0,0>, v=< 2,0,0>, a=<-1,0,0>
|
||||||
p=< 4,0,0>, v=< 0,0,0>, a=<-2,0,0>
|
p=< 4,0,0>, v=< 0,0,0>, a=<-2,0,0>
|
||||||
'''.strip()
|
""".strip()
|
||||||
assert self.solution.solve(puzzle_input, 4) == 0
|
assert self.solution.solve(puzzle_input, 4) == 0
|
||||||
|
|
||||||
def test_something(self):
|
def test_something(self):
|
||||||
puzzle_input = '''
|
puzzle_input = """
|
||||||
p=<-6,0,0>, v=< 3,0,0>, a=< 0,0,0>
|
p=<-6,0,0>, v=< 3,0,0>, a=< 0,0,0>
|
||||||
p=<-4,0,0>, v=< 2,0,0>, a=< 0,0,0>
|
p=<-4,0,0>, v=< 2,0,0>, a=< 0,0,0>
|
||||||
p=<-2,0,0>, v=< 1,0,0>, a=< 0,0,0>
|
p=<-2,0,0>, v=< 1,0,0>, a=< 0,0,0>
|
||||||
p=< 3,0,0>, v=<-1,0,0>, a=< 0,0,0>
|
p=< 3,0,0>, v=<-1,0,0>, a=< 0,0,0>
|
||||||
'''.strip()
|
""".strip()
|
||||||
assert self.solution.solve_again(puzzle_input, 4) == 1
|
assert self.solution.solve_again(puzzle_input, 4) == 1
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
||||||
|
|
@ -8,12 +8,12 @@ class Day21TestCase(unittest.TestCase):
|
||||||
self.solution = Solution()
|
self.solution = Solution()
|
||||||
|
|
||||||
def test_something(self):
|
def test_something(self):
|
||||||
puzzle_input = '''
|
puzzle_input = """
|
||||||
../.# => ##./#../...
|
../.# => ##./#../...
|
||||||
.#./..#/### => #..#/..../..../#..#
|
.#./..#/### => #..#/..../..../#..#
|
||||||
'''.strip()
|
""".strip()
|
||||||
assert self.solution.solve(puzzle_input, 2) == 12
|
assert self.solution.solve(puzzle_input, 2) == 12
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
||||||
|
|
@ -26,5 +26,6 @@ class Day22TestCase(unittest.TestCase):
|
||||||
assert self.solution.solve_again(puzzle_input, 100) == 26
|
assert self.solution.solve_again(puzzle_input, 100) == 26
|
||||||
assert self.solution.solve_again(puzzle_input, 10000000) == 2511944
|
assert self.solution.solve_again(puzzle_input, 10000000) == 2511944
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
||||||
48
2024-python/README.md
Normal file
48
2024-python/README.md
Normal file
|
|
@ -0,0 +1,48 @@
|
||||||
|
# Advent of Code 2024
|
||||||
|
|
||||||
|
Solutions for #aoc2024 in Python 3 (3.12.7).
|
||||||
|
|
||||||
|
Programming setup:
|
||||||
|
|
||||||
|
- Lenovo Thinkpad X260
|
||||||
|
- Arch Linux with Hyprland
|
||||||
|
- Zed editor (Ruff, Pyright)
|
||||||
|
- Firefox
|
||||||
|
- Alacritty
|
||||||
|
|
||||||
|
## Help scripts
|
||||||
|
|
||||||
|
Display all solved puzzles:
|
||||||
|
|
||||||
|
python aoc.py
|
||||||
|
|
||||||
|
To bootstrap a new puzzle (creates `input/<day_no>.txt` and `output/day_<day_no>.py`):
|
||||||
|
|
||||||
|
python aoc.py <day_no> new
|
||||||
|
|
||||||
|
Manually copy the puzzle input from https://adventofcode.com and paste it in `input/<day_no>.txt`
|
||||||
|
to start coding.
|
||||||
|
|
||||||
|
wl-paste > input/<day_no>.txt
|
||||||
|
|
||||||
|
Solve separate puzzle (replace `XX` with the puzzle number):
|
||||||
|
|
||||||
|
python -m output.day_XX
|
||||||
|
|
||||||
|
Solve separate puzzle using stdin (replace `XX` with the puzzle number):
|
||||||
|
|
||||||
|
wl-paste | python -m output.day_XX
|
||||||
|
cat tmpfile | python -m output.day_XX
|
||||||
|
|
||||||
|
Execute separate puzzle on file save (replace `XX` with the puzzle number):
|
||||||
|
|
||||||
|
ls output/*.py | entr -c -s 'wlpaste | python -m output.day_XX'
|
||||||
|
ls output/*.py | entr -c -s 'cat tmpfile | python -m output.day_XX'
|
||||||
|
ls output/*.py | entr -c -r python -m output.day_XX
|
||||||
|
|
||||||
|
(requires `entr` and `wl-paste`, Mac users can instead use `pbpaste`. If you
|
||||||
|
prefer X at Linux, use `xclip -selection clipboard -o`).
|
||||||
|
|
||||||
|
To lint files:
|
||||||
|
|
||||||
|
ls output/*.py | entr -r -c flake8 output --ignore=E741,E501,E203
|
||||||
115
2024-python/aoc.py
Normal file
115
2024-python/aoc.py
Normal file
|
|
@ -0,0 +1,115 @@
|
||||||
|
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("")
|
||||||
195
2024-python/output/__init__.py
Normal file
195
2024-python/output/__init__.py
Normal file
|
|
@ -0,0 +1,195 @@
|
||||||
|
import re
|
||||||
|
|
||||||
|
# Directions/Adjacents for 2D matrices, in the order UP, RIGHT, DOWN, LEFT
|
||||||
|
D = [
|
||||||
|
(-1, 0),
|
||||||
|
(0, 1),
|
||||||
|
(1, 0),
|
||||||
|
(0, -1),
|
||||||
|
]
|
||||||
|
|
||||||
|
Di = [
|
||||||
|
(-1, -1),
|
||||||
|
(-1, 1),
|
||||||
|
(1, -1),
|
||||||
|
(1, 1),
|
||||||
|
]
|
||||||
|
|
||||||
|
# Directions for 2D matrices, as a dict with keys U, R, D, L
|
||||||
|
DD = {
|
||||||
|
"U": (-1, 0),
|
||||||
|
"R": (0, 1),
|
||||||
|
"D": (1, 0),
|
||||||
|
"L": (0, -1),
|
||||||
|
}
|
||||||
|
|
||||||
|
DDa = {
|
||||||
|
"^": (-1, 0),
|
||||||
|
">": (0, 1),
|
||||||
|
"v": (1, 0),
|
||||||
|
"<": (0, -1),
|
||||||
|
}
|
||||||
|
|
||||||
|
# Adjacent relative positions including diagonals for 2D matrices, in the order NW, N, NE, W, E, SW, S, SE
|
||||||
|
ADJ = [
|
||||||
|
(-1, -1),
|
||||||
|
(-1, 0),
|
||||||
|
(1, -1),
|
||||||
|
(0, -1),
|
||||||
|
(0, 1),
|
||||||
|
(1, 1),
|
||||||
|
(1, 0),
|
||||||
|
(1, -1),
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def ints(s):
|
||||||
|
"""Extract all integers from a string"""
|
||||||
|
return [int(n) for n in re.findall(r"\d+", s)]
|
||||||
|
|
||||||
|
|
||||||
|
def sints(s):
|
||||||
|
"""Extract all signed integers from a string"""
|
||||||
|
return [int(n) for n in re.findall(r"-?\d+", s)]
|
||||||
|
|
||||||
|
|
||||||
|
def mhd(a, b):
|
||||||
|
"""Calculates the Manhattan distance between 2 positions in the format (y, x) or (x, y)"""
|
||||||
|
ar, ac = a
|
||||||
|
br, bc = b
|
||||||
|
return abs(ar - br) + abs(ac - bc)
|
||||||
|
|
||||||
|
|
||||||
|
def matrix(d):
|
||||||
|
"""Transform a string into an iterable matrix. Returns the matrix, row count and col count"""
|
||||||
|
m = [tuple(r) for r in d.split()]
|
||||||
|
return m, len(m), len(m[0])
|
||||||
|
|
||||||
|
|
||||||
|
def mdbg(m):
|
||||||
|
"""Print-debug a matrix"""
|
||||||
|
for r in m:
|
||||||
|
print("".join(r))
|
||||||
|
|
||||||
|
|
||||||
|
def vdbg(seen, h, w):
|
||||||
|
"""Print-debug visited positions of a matrix"""
|
||||||
|
for r in range(h):
|
||||||
|
print("".join(["#" if (r, c) in seen else " " for c in range(w)]))
|
||||||
|
|
||||||
|
|
||||||
|
def cw(y, x):
|
||||||
|
"""Flip a (y, x) direction counterwise: U->R, R->D, D->L, L->U.
|
||||||
|
|
||||||
|
>>> cw(-1, 0)
|
||||||
|
(0, 1)
|
||||||
|
>>> cw(0, 1)
|
||||||
|
(1, 0)
|
||||||
|
>>> cw(1, 0)
|
||||||
|
(0, -1)
|
||||||
|
>>> cw(0, -1)
|
||||||
|
(-1, 0)
|
||||||
|
"""
|
||||||
|
return (x, y) if y == 0 else (x, -y)
|
||||||
|
|
||||||
|
|
||||||
|
def ccw(y, x):
|
||||||
|
"""Flip a (y, x) direction counterwise: U->L, L->D, D->R, R->U.
|
||||||
|
|
||||||
|
>>> ccw(-1, 0)
|
||||||
|
(0, -1)
|
||||||
|
>>> ccw(0, -1)
|
||||||
|
(1, 0)
|
||||||
|
>>> ccw(1, 0)
|
||||||
|
(0, 1)
|
||||||
|
>>> ccw(0, 1)
|
||||||
|
(-1, 0)
|
||||||
|
"""
|
||||||
|
return (x, y) if x == 0 else (-x, y)
|
||||||
|
|
||||||
|
|
||||||
|
def bfs(S, E=None):
|
||||||
|
"""BFS algorithm, equal weighted nodes"""
|
||||||
|
seen = set()
|
||||||
|
q = [(S, 0)]
|
||||||
|
g = {} # graph, required to be provided at some point
|
||||||
|
while q:
|
||||||
|
m, w = q.pop(0)
|
||||||
|
if m in seen:
|
||||||
|
continue
|
||||||
|
seen.add(m)
|
||||||
|
# investigate here
|
||||||
|
for s in g[m]:
|
||||||
|
q.append((s, w + 1))
|
||||||
|
# return insights
|
||||||
|
|
||||||
|
|
||||||
|
def mhd_search(r, c, R=20):
|
||||||
|
"""returns all coords that are within R manhattan distance from (r,c)"""
|
||||||
|
p = set()
|
||||||
|
for d in range(1, R + 1):
|
||||||
|
p.add((r, c + d))
|
||||||
|
p.add((r, c - d))
|
||||||
|
p.add((r + d, c))
|
||||||
|
p.add((r - d, c))
|
||||||
|
for dd in range(d):
|
||||||
|
p.add((r - dd, c - d + dd))
|
||||||
|
p.add((r + dd, c - d + dd))
|
||||||
|
p.add((r - dd, c - dd + d))
|
||||||
|
p.add((r + dd, c - dd + d))
|
||||||
|
return p
|
||||||
|
|
||||||
|
|
||||||
|
def dijkstras(grid, start, target):
|
||||||
|
"""
|
||||||
|
1. Create an array that holds the distance of each vertex from the starting
|
||||||
|
vertex. Initially, set this distance to infinity for all vertices except
|
||||||
|
the starting vertex which should be set to 0.
|
||||||
|
2. Create a priority queue (heap) and insert the starting vertex with its
|
||||||
|
distance of 0.
|
||||||
|
3. While there are still vertices left in the priority queue, select the vertex
|
||||||
|
with the smallest recorded distance from the starting vertex and visit its
|
||||||
|
neighboring vertices.
|
||||||
|
4. For each neighboring vertex, check if it is visited already or not. If it
|
||||||
|
isn’t visited yet, calculate its tentative distance by adding its weight
|
||||||
|
to the smallest distance found so far for its parent/previous node
|
||||||
|
(starting vertex in case of first-level vertices).
|
||||||
|
5. If this tentative distance is smaller than previously recorded value
|
||||||
|
(if any), update it in our ‘distances’ array.
|
||||||
|
6. Finally, add this visited vertex with its updated distance to our priority
|
||||||
|
queue and repeat step-3 until we have reached our destination or exhausted
|
||||||
|
all nodes.
|
||||||
|
"""
|
||||||
|
import heapq
|
||||||
|
|
||||||
|
target = max(grid)
|
||||||
|
seen = set()
|
||||||
|
queue = [(start, 0)]
|
||||||
|
while queue:
|
||||||
|
cost, pos, direction, steps = heapq.heappop(queue)
|
||||||
|
y, x = pos
|
||||||
|
dy, dx = direction
|
||||||
|
|
||||||
|
if pos == target:
|
||||||
|
return cost
|
||||||
|
|
||||||
|
if ((pos, "and stuff")) in seen:
|
||||||
|
continue
|
||||||
|
|
||||||
|
seen.add((pos, "and stuff"))
|
||||||
|
|
||||||
|
neighbors = []
|
||||||
|
for n in neighbors:
|
||||||
|
heapq.heappush(queue, ("stuffs"))
|
||||||
|
|
||||||
|
return -1
|
||||||
|
|
||||||
|
|
||||||
|
def bk(graph, p, r=set(), x=set()):
|
||||||
|
"""Bron-Kerbosch algoritm, no pivot: https://en.wikipedia.org/wiki/Bron%E2%80%93Kerbosch_algorithm"""
|
||||||
|
if not p and not x:
|
||||||
|
yield r
|
||||||
|
while p:
|
||||||
|
v = p.pop()
|
||||||
|
yield from bk(graph, p & set(graph[v]), r | {v}, x & graph[v])
|
||||||
|
x.add(v)
|
||||||
20
2024-python/output/day_01.py
Normal file
20
2024-python/output/day_01.py
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
from output import ints
|
||||||
|
|
||||||
|
|
||||||
|
def solve(puzzle_input):
|
||||||
|
left, right = [sorted(col) for col in zip(*map(ints, puzzle_input.splitlines()))]
|
||||||
|
|
||||||
|
p1 = sum(abs(l - r) for l, r in zip(left, right))
|
||||||
|
p2 = sum(k * right.count(k) for k in left)
|
||||||
|
|
||||||
|
return p1, p2
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
with open("./input/01.txt", "r") as f:
|
||||||
|
puzzle_input = f.read().strip()
|
||||||
|
|
||||||
|
p1, p2 = solve(puzzle_input)
|
||||||
|
|
||||||
|
print(p1)
|
||||||
|
print(p2)
|
||||||
34
2024-python/output/day_02.py
Normal file
34
2024-python/output/day_02.py
Normal file
|
|
@ -0,0 +1,34 @@
|
||||||
|
from output import ints
|
||||||
|
|
||||||
|
|
||||||
|
def solve(data):
|
||||||
|
reports = [ints(line) for line in data.splitlines()]
|
||||||
|
p1 = 0
|
||||||
|
p2 = 0
|
||||||
|
|
||||||
|
for report in reports:
|
||||||
|
if issafe(report):
|
||||||
|
p1 += 1
|
||||||
|
|
||||||
|
for i in range(len(report)):
|
||||||
|
if issafe(report[:i] + report[i + 1 :]):
|
||||||
|
p2 += 1
|
||||||
|
break
|
||||||
|
|
||||||
|
return p1, p2
|
||||||
|
|
||||||
|
|
||||||
|
def issafe(report):
|
||||||
|
diffs = [a - b for a, b in zip(report, report[1:])]
|
||||||
|
|
||||||
|
return all(-3 <= d <= -1 for d in diffs) or all(1 <= d <= 3 for d in diffs)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
with open("./input/02.txt", "r") as f:
|
||||||
|
inp = f.read().strip()
|
||||||
|
|
||||||
|
p1, p2 = solve(inp)
|
||||||
|
|
||||||
|
print(p1)
|
||||||
|
print(p2)
|
||||||
26
2024-python/output/day_03.py
Normal file
26
2024-python/output/day_03.py
Normal file
|
|
@ -0,0 +1,26 @@
|
||||||
|
import re
|
||||||
|
from math import prod
|
||||||
|
|
||||||
|
|
||||||
|
def solve(data):
|
||||||
|
needle = re.compile(r"mul\((\d{1,3}),(\d{1,3})\)")
|
||||||
|
|
||||||
|
p1 = sum(prod(map(int, factors)) for factors in re.findall(needle, data))
|
||||||
|
|
||||||
|
p2 = sum(
|
||||||
|
sum(prod(map(int, factors)) for factors in re.findall(needle, chunk))
|
||||||
|
for chunk in data.split("do")
|
||||||
|
if not chunk.startswith("n't")
|
||||||
|
)
|
||||||
|
|
||||||
|
return p1, p2
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
with open("./input/03.txt", "r") as f:
|
||||||
|
inp = f.read().strip()
|
||||||
|
|
||||||
|
p1, p2 = solve(inp)
|
||||||
|
|
||||||
|
print(p1)
|
||||||
|
print(p2)
|
||||||
92
2024-python/output/day_04.py
Normal file
92
2024-python/output/day_04.py
Normal file
|
|
@ -0,0 +1,92 @@
|
||||||
|
import re
|
||||||
|
|
||||||
|
|
||||||
|
def solve(data):
|
||||||
|
row_count = len(data.split())
|
||||||
|
col_count = len(data.split()[0])
|
||||||
|
grid = [c for r in data.split() for c in r]
|
||||||
|
grid_rotated = [c for r in zip(*data.split()[::-1]) for c in r]
|
||||||
|
needle = r"(?=(XMAS|SAMX))"
|
||||||
|
|
||||||
|
p1 = len(re.findall(needle, data))
|
||||||
|
p1 += len(
|
||||||
|
re.findall(
|
||||||
|
needle,
|
||||||
|
"\n".join(["".join(r) for r in list(zip(*data.split()))]),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
for cells, o in [
|
||||||
|
(grid, col_count),
|
||||||
|
(grid[::-1], col_count),
|
||||||
|
(grid_rotated, row_count),
|
||||||
|
(grid_rotated[::-1], row_count),
|
||||||
|
]:
|
||||||
|
p1 += sum(
|
||||||
|
all(
|
||||||
|
[
|
||||||
|
i % o < (o - 3),
|
||||||
|
cells[i] == "X",
|
||||||
|
cells[i + o + 1] == "M",
|
||||||
|
cells[i + 2 * (o + 1)] == "A",
|
||||||
|
cells[i + 3 * (o + 1)] == "S",
|
||||||
|
]
|
||||||
|
)
|
||||||
|
for i in range(len(cells) - 3 * (o + 1))
|
||||||
|
)
|
||||||
|
|
||||||
|
p2 = sum(
|
||||||
|
[
|
||||||
|
1 <= (i % col_count) < col_count - 1
|
||||||
|
and grid[i] == "A"
|
||||||
|
and any(
|
||||||
|
[
|
||||||
|
all(
|
||||||
|
[
|
||||||
|
grid[i - col_count - 1] == "M",
|
||||||
|
grid[i - col_count + 1] == "M",
|
||||||
|
grid[i + col_count - 1] == "S",
|
||||||
|
grid[i + col_count + 1] == "S",
|
||||||
|
]
|
||||||
|
),
|
||||||
|
all(
|
||||||
|
[
|
||||||
|
grid[i - col_count - 1] == "S",
|
||||||
|
grid[i - col_count + 1] == "S",
|
||||||
|
grid[i + col_count - 1] == "M",
|
||||||
|
grid[i + col_count + 1] == "M",
|
||||||
|
]
|
||||||
|
),
|
||||||
|
all(
|
||||||
|
[
|
||||||
|
grid[i - col_count - 1] == "M",
|
||||||
|
grid[i - col_count + 1] == "S",
|
||||||
|
grid[i + col_count - 1] == "M",
|
||||||
|
grid[i + col_count + 1] == "S",
|
||||||
|
]
|
||||||
|
),
|
||||||
|
all(
|
||||||
|
[
|
||||||
|
grid[i - col_count - 1] == "S",
|
||||||
|
grid[i - col_count + 1] == "M",
|
||||||
|
grid[i + col_count - 1] == "S",
|
||||||
|
grid[i + col_count + 1] == "M",
|
||||||
|
]
|
||||||
|
),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
for i in range(col_count + 1, len(grid) - col_count - 1)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
return p1, p2
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
with open("./input/04.txt", "r") as f:
|
||||||
|
inp = f.read().strip()
|
||||||
|
|
||||||
|
p1, p2 = solve(inp)
|
||||||
|
|
||||||
|
print(p1)
|
||||||
|
print(p2)
|
||||||
77
2024-python/output/day_05.py
Normal file
77
2024-python/output/day_05.py
Normal file
|
|
@ -0,0 +1,77 @@
|
||||||
|
from output import ints
|
||||||
|
|
||||||
|
|
||||||
|
def solve(data):
|
||||||
|
rules, pagelists = data.split("\n\n")
|
||||||
|
rules, pagelists = (
|
||||||
|
set(map(lambda line: tuple(ints(line)), rules.splitlines())),
|
||||||
|
list(map(lambda line: ints(line), pagelists.splitlines())),
|
||||||
|
)
|
||||||
|
|
||||||
|
corrects = []
|
||||||
|
incorrects = []
|
||||||
|
adjusted = []
|
||||||
|
|
||||||
|
for pagelist in pagelists:
|
||||||
|
ok = True
|
||||||
|
|
||||||
|
for i, page in enumerate(pagelist):
|
||||||
|
prevpages, nextpages = pagelist[:i], pagelist[i + 1 :]
|
||||||
|
|
||||||
|
if not all((pp, page) in rules for pp in prevpages) or not all(
|
||||||
|
(page, np) in rules for np in nextpages
|
||||||
|
):
|
||||||
|
ok = False
|
||||||
|
break
|
||||||
|
|
||||||
|
if ok:
|
||||||
|
corrects.append(pagelist)
|
||||||
|
else:
|
||||||
|
incorrects.append(pagelist)
|
||||||
|
|
||||||
|
while incorrects:
|
||||||
|
pagelist = incorrects.pop(0)
|
||||||
|
changed = False
|
||||||
|
|
||||||
|
for i, page in enumerate(pagelist):
|
||||||
|
prevpages, nextpages = pagelist[:i], pagelist[i + 1 :]
|
||||||
|
|
||||||
|
for prevpage in prevpages:
|
||||||
|
if (prevpage, page) in rules:
|
||||||
|
continue
|
||||||
|
|
||||||
|
nextpages.append(prevpages.pop())
|
||||||
|
pagelist = prevpages + [page] + nextpages
|
||||||
|
changed = True
|
||||||
|
break
|
||||||
|
|
||||||
|
if not changed:
|
||||||
|
for nextpage in nextpages:
|
||||||
|
if (page, nextpage) in rules:
|
||||||
|
continue
|
||||||
|
prevpages.append(nextpages.pop())
|
||||||
|
pagelist = prevpages + [page] + nextpages
|
||||||
|
changed = True
|
||||||
|
break
|
||||||
|
|
||||||
|
if changed:
|
||||||
|
break
|
||||||
|
|
||||||
|
if not changed:
|
||||||
|
adjusted.append(pagelist)
|
||||||
|
else:
|
||||||
|
incorrects.append(pagelist)
|
||||||
|
|
||||||
|
p1 = sum(c[len(c) // 2] for c in corrects)
|
||||||
|
p2 = sum(r[len(r) // 2] for r in adjusted)
|
||||||
|
return p1, p2
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
with open("./input/05.txt", "r") as f:
|
||||||
|
inp = f.read().strip()
|
||||||
|
|
||||||
|
p1, p2 = solve(inp)
|
||||||
|
|
||||||
|
print(p1)
|
||||||
|
print(p2)
|
||||||
56
2024-python/output/day_06.py
Normal file
56
2024-python/output/day_06.py
Normal file
|
|
@ -0,0 +1,56 @@
|
||||||
|
from output import cw, matrix
|
||||||
|
|
||||||
|
|
||||||
|
def solve(data):
|
||||||
|
M, H, W = matrix(data)
|
||||||
|
|
||||||
|
y, x = [(r, c) for r in range(H) for c in range(W) if M[r][c] == "^"][0]
|
||||||
|
delta_y, delta_x = (-1, 0)
|
||||||
|
seen, _ = travel(M, H, W, y, x, delta_y, delta_x)
|
||||||
|
|
||||||
|
p1 = len(seen)
|
||||||
|
|
||||||
|
p2 = 0
|
||||||
|
for row, col in seen:
|
||||||
|
grid = [list(row) for row in M]
|
||||||
|
grid[row][col] = "#"
|
||||||
|
_, loop_found = travel(grid, H, W, y, x, delta_y, delta_x)
|
||||||
|
|
||||||
|
if loop_found:
|
||||||
|
p2 += 1
|
||||||
|
|
||||||
|
return p1, p2
|
||||||
|
|
||||||
|
|
||||||
|
def travel(M, H, W, y, x, delta_y, delta_x):
|
||||||
|
seen = set()
|
||||||
|
hit = set()
|
||||||
|
loop_found = False
|
||||||
|
|
||||||
|
while True:
|
||||||
|
seen.add((y, x))
|
||||||
|
|
||||||
|
if y == 0 or y == H - 1 or x == 0 or x == W - 1:
|
||||||
|
break
|
||||||
|
|
||||||
|
if M[y + delta_y][x + delta_x] == "#":
|
||||||
|
if (y + delta_y, x + delta_x, delta_y, delta_x) in hit:
|
||||||
|
loop_found = True
|
||||||
|
break
|
||||||
|
|
||||||
|
hit.add((y + delta_y, x + delta_x, delta_y, delta_x))
|
||||||
|
delta_y, delta_x = cw(delta_y, delta_x)
|
||||||
|
else:
|
||||||
|
y, x = y + delta_y, x + delta_x
|
||||||
|
|
||||||
|
return seen, loop_found
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
with open("./input/06.txt", "r") as f:
|
||||||
|
inp = f.read().strip()
|
||||||
|
|
||||||
|
p1, p2 = solve(inp)
|
||||||
|
|
||||||
|
print(p1)
|
||||||
|
print(p2)
|
||||||
43
2024-python/output/day_07.py
Normal file
43
2024-python/output/day_07.py
Normal file
|
|
@ -0,0 +1,43 @@
|
||||||
|
from collections import deque
|
||||||
|
|
||||||
|
from output import ints
|
||||||
|
|
||||||
|
|
||||||
|
def solve(data):
|
||||||
|
mul_add = calculate(data)
|
||||||
|
mul_add_concat = calculate(data, concat=True)
|
||||||
|
return mul_add, mul_add_concat
|
||||||
|
|
||||||
|
|
||||||
|
def calculate(data, concat=False):
|
||||||
|
values = list()
|
||||||
|
for nums in [ints(line) for line in data.splitlines()]:
|
||||||
|
T, start, *nums = nums
|
||||||
|
E = len(nums)
|
||||||
|
q = deque([(0, start)])
|
||||||
|
ok = False
|
||||||
|
while q:
|
||||||
|
i, value = q.pop()
|
||||||
|
if i == E:
|
||||||
|
if value == T:
|
||||||
|
ok = True
|
||||||
|
continue
|
||||||
|
if (a := value + nums[i]) <= T:
|
||||||
|
q.append((i + 1, a))
|
||||||
|
if (b := value * nums[i]) <= T:
|
||||||
|
q.append((i + 1, b))
|
||||||
|
if concat and (c := int(f"{value}{nums[i]}")) <= T:
|
||||||
|
q.append((i + 1, c))
|
||||||
|
if ok:
|
||||||
|
values.append(T)
|
||||||
|
return sum(values)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
with open("./input/07.txt", "r") as f:
|
||||||
|
inp = f.read().strip()
|
||||||
|
|
||||||
|
p1, p2 = solve(inp)
|
||||||
|
|
||||||
|
print(p1)
|
||||||
|
print(p2)
|
||||||
44
2024-python/output/day_08.py
Normal file
44
2024-python/output/day_08.py
Normal file
|
|
@ -0,0 +1,44 @@
|
||||||
|
from collections import defaultdict, deque
|
||||||
|
from itertools import permutations
|
||||||
|
|
||||||
|
from output import matrix
|
||||||
|
|
||||||
|
|
||||||
|
def solve(data):
|
||||||
|
grid, H, W = matrix(data)
|
||||||
|
antennas = defaultdict(list)
|
||||||
|
for row, col in [
|
||||||
|
(row, col)
|
||||||
|
for row, row_values in enumerate(grid)
|
||||||
|
for col, _vc in enumerate(row_values)
|
||||||
|
]:
|
||||||
|
if (id := grid[row][col]) != ".":
|
||||||
|
antennas[id].append((row, col))
|
||||||
|
adjacent_locs = set()
|
||||||
|
all_antinodes = set()
|
||||||
|
for k, v in antennas.items():
|
||||||
|
for a, b in permutations(v, r=2):
|
||||||
|
all_antinodes.add(a)
|
||||||
|
a1, a2 = a
|
||||||
|
b1, b2 = b
|
||||||
|
delta_y, delta_x = a1 - b1, a2 - b2
|
||||||
|
y, x = a1 + delta_y, a2 + delta_x
|
||||||
|
if 0 <= y < H and 0 <= x < W:
|
||||||
|
adjacent_locs.add((y, x))
|
||||||
|
queue = deque([(y, x, delta_y, delta_x)])
|
||||||
|
while queue:
|
||||||
|
y, x, delta_y, delta_x = queue.popleft()
|
||||||
|
if 0 <= y < H and 0 <= x < W:
|
||||||
|
all_antinodes.add((y, x))
|
||||||
|
queue.append((y + delta_y, x + delta_x, delta_y, delta_x))
|
||||||
|
return len(adjacent_locs), len(all_antinodes)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
with open("./input/08.txt", "r") as f:
|
||||||
|
inp = f.read().strip()
|
||||||
|
|
||||||
|
p1, p2 = solve(inp)
|
||||||
|
|
||||||
|
print(p1)
|
||||||
|
print(p2)
|
||||||
67
2024-python/output/day_09.py
Normal file
67
2024-python/output/day_09.py
Normal file
|
|
@ -0,0 +1,67 @@
|
||||||
|
from collections import deque
|
||||||
|
|
||||||
|
|
||||||
|
def solve(data):
|
||||||
|
blocks_checksum = defrag(data)
|
||||||
|
files_checksum = defrag(data, True)
|
||||||
|
return blocks_checksum, files_checksum
|
||||||
|
|
||||||
|
|
||||||
|
def defrag(data, move_files=False):
|
||||||
|
expected_seq_len = sum(map(int, data[::2]))
|
||||||
|
seq = dict()
|
||||||
|
gaps = dict()
|
||||||
|
q = deque([])
|
||||||
|
i = 0
|
||||||
|
for fileid, pair in enumerate(zip(data[::2], data[1::2] + "0")):
|
||||||
|
size, free = map(int, pair)
|
||||||
|
q.append((i, fileid, size))
|
||||||
|
for _ in range(int(size)):
|
||||||
|
seq[i] = fileid
|
||||||
|
i += 1
|
||||||
|
gaps[i] = free
|
||||||
|
i += int(free)
|
||||||
|
while q:
|
||||||
|
i, fileid, size = q.pop()
|
||||||
|
if move_files:
|
||||||
|
updated = False
|
||||||
|
for pos, space in gaps.items():
|
||||||
|
if pos > i:
|
||||||
|
continue
|
||||||
|
if size <= space:
|
||||||
|
rem = space - size
|
||||||
|
updated = True
|
||||||
|
break
|
||||||
|
if updated:
|
||||||
|
for k in range(size):
|
||||||
|
seq[pos + k] = fileid
|
||||||
|
for k in range(i, i + size):
|
||||||
|
del seq[k]
|
||||||
|
del gaps[pos]
|
||||||
|
gaps[pos + size] = rem
|
||||||
|
gaps = dict(sorted(gaps.items(), key=lambda x: x[0]))
|
||||||
|
else:
|
||||||
|
move = False
|
||||||
|
for j in range(size):
|
||||||
|
for k in range(expected_seq_len):
|
||||||
|
if k not in seq:
|
||||||
|
seq[k] = fileid
|
||||||
|
del seq[i + j]
|
||||||
|
move = True
|
||||||
|
break
|
||||||
|
if not move:
|
||||||
|
break
|
||||||
|
return sum(
|
||||||
|
pos * id
|
||||||
|
for pos, id in enumerate(seq.get(fileid, 0) for fileid in range(max(seq) + 1))
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
with open("./input/09.txt", "r") as f:
|
||||||
|
inp = f.read().strip()
|
||||||
|
|
||||||
|
p1, p2 = solve(inp)
|
||||||
|
|
||||||
|
print(p1)
|
||||||
|
print(p2)
|
||||||
41
2024-python/output/day_10.py
Normal file
41
2024-python/output/day_10.py
Normal file
|
|
@ -0,0 +1,41 @@
|
||||||
|
from collections import defaultdict, deque
|
||||||
|
|
||||||
|
from output import D, matrix
|
||||||
|
|
||||||
|
|
||||||
|
def solve(data):
|
||||||
|
grid, H, W = matrix(data)
|
||||||
|
starts = [
|
||||||
|
(r, c, int(grid[r][c]), (r, c))
|
||||||
|
for r, y in enumerate(grid)
|
||||||
|
for c, x in enumerate(y)
|
||||||
|
if x == "0"
|
||||||
|
]
|
||||||
|
queue = deque(starts)
|
||||||
|
scores = defaultdict(set)
|
||||||
|
ratings = defaultdict(int)
|
||||||
|
while queue:
|
||||||
|
r, c, value, start = queue.popleft()
|
||||||
|
if value == 9:
|
||||||
|
scores[start].add((r, c))
|
||||||
|
ratings[start] += 1
|
||||||
|
continue
|
||||||
|
for dy, dx in D:
|
||||||
|
if not (0 <= (qr := r + dy) < H and 0 <= (qc := c + dx) < W):
|
||||||
|
continue
|
||||||
|
if int(grid[qr][qc]) - value != 1:
|
||||||
|
continue
|
||||||
|
queue.append((qr, qc, int(grid[qr][qc]), start))
|
||||||
|
scores_sum = sum(len(t) for t in scores.values())
|
||||||
|
ratings_sum = sum(ratings.values())
|
||||||
|
return scores_sum, ratings_sum
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
with open("./input/10.txt", "r") as f:
|
||||||
|
inp = f.read().strip()
|
||||||
|
|
||||||
|
p1, p2 = solve(inp)
|
||||||
|
|
||||||
|
print(p1)
|
||||||
|
print(p2)
|
||||||
38
2024-python/output/day_11.py
Normal file
38
2024-python/output/day_11.py
Normal file
|
|
@ -0,0 +1,38 @@
|
||||||
|
from collections import defaultdict
|
||||||
|
|
||||||
|
from output import ints
|
||||||
|
|
||||||
|
|
||||||
|
def solve(data):
|
||||||
|
state = defaultdict(int)
|
||||||
|
for e in ints(data):
|
||||||
|
state[e] += 1
|
||||||
|
for lap in range(75):
|
||||||
|
if lap == 25:
|
||||||
|
stones_count_25 = sum(state.values())
|
||||||
|
queue = list(map(lambda x: x, state.items()))
|
||||||
|
for e, delta in queue:
|
||||||
|
if e == 0:
|
||||||
|
state[1] += delta
|
||||||
|
state[0] -= delta
|
||||||
|
elif len(str(e)) % 2 == 0:
|
||||||
|
i = len(str(e)) // 2
|
||||||
|
l, r = int(str(e)[:i]), int(str(e)[i:])
|
||||||
|
state[l] += delta
|
||||||
|
state[r] += delta
|
||||||
|
state[e] -= delta
|
||||||
|
else:
|
||||||
|
state[e * 2024] += delta
|
||||||
|
state[e] -= delta
|
||||||
|
stones_count_75 = sum(state.values())
|
||||||
|
return stones_count_25, stones_count_75
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
with open("./input/11.txt", "r") as f:
|
||||||
|
inp = f.read().strip()
|
||||||
|
|
||||||
|
p1, p2 = solve(inp)
|
||||||
|
|
||||||
|
print(p1)
|
||||||
|
print(p2)
|
||||||
73
2024-python/output/day_12.py
Normal file
73
2024-python/output/day_12.py
Normal file
|
|
@ -0,0 +1,73 @@
|
||||||
|
from collections import deque
|
||||||
|
|
||||||
|
from output import D, Di, cw, matrix
|
||||||
|
|
||||||
|
|
||||||
|
def solve(data):
|
||||||
|
grid, H, W = matrix(data)
|
||||||
|
fence_cost = 0
|
||||||
|
fence_cost_w_discount = 0
|
||||||
|
seen = set()
|
||||||
|
for r in range(H):
|
||||||
|
for c in range(W):
|
||||||
|
if (r, c) in seen:
|
||||||
|
continue
|
||||||
|
id = grid[r][c]
|
||||||
|
regions = set()
|
||||||
|
perimeters = 0
|
||||||
|
q = deque([(r, c)])
|
||||||
|
while q:
|
||||||
|
rc = q.popleft()
|
||||||
|
if rc in seen:
|
||||||
|
continue
|
||||||
|
seen.add(rc)
|
||||||
|
regions.add(rc)
|
||||||
|
y, x = rc
|
||||||
|
for delta_y, delta_x in D:
|
||||||
|
yn, xn = y + delta_y, x + delta_x
|
||||||
|
if (0 <= yn < H and 0 <= xn < W) and grid[yn][xn] == id:
|
||||||
|
q.append((yn, xn))
|
||||||
|
else:
|
||||||
|
perimeters += 1
|
||||||
|
if len(regions) <= 2:
|
||||||
|
corners = 4
|
||||||
|
else:
|
||||||
|
corners = count_corners(regions)
|
||||||
|
fence_cost += len(regions) * perimeters
|
||||||
|
fence_cost_w_discount += len(regions) * corners
|
||||||
|
return fence_cost, fence_cost_w_discount
|
||||||
|
|
||||||
|
|
||||||
|
def count_corners(region):
|
||||||
|
corners = 0
|
||||||
|
for y, x in region:
|
||||||
|
for delta_y, delta_x in D:
|
||||||
|
# convex corners: one horisontal and one vertical
|
||||||
|
# neighbor are not members of region
|
||||||
|
delta_ycw, delta_xcw = cw(delta_y, delta_x)
|
||||||
|
if (y + delta_y, x + delta_x) not in region and (
|
||||||
|
y + delta_ycw,
|
||||||
|
x + delta_xcw,
|
||||||
|
) not in region:
|
||||||
|
corners += 1
|
||||||
|
for delta_y, delta_x in Di:
|
||||||
|
# concave corners: the diagonal neighbor are not
|
||||||
|
# member of region, but the connected horisontal
|
||||||
|
# and vertical neighbors are
|
||||||
|
if (
|
||||||
|
(y + delta_y, x + delta_x) not in region
|
||||||
|
and (y + delta_y, x) in region
|
||||||
|
and (y, x + delta_x) in region
|
||||||
|
):
|
||||||
|
corners += 1
|
||||||
|
return corners
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
with open("./input/12.txt", "r") as f:
|
||||||
|
inp = f.read().strip()
|
||||||
|
|
||||||
|
p1, p2 = solve(inp)
|
||||||
|
|
||||||
|
print(p1)
|
||||||
|
print(p2)
|
||||||
36
2024-python/output/day_13.py
Normal file
36
2024-python/output/day_13.py
Normal file
|
|
@ -0,0 +1,36 @@
|
||||||
|
from output import ints
|
||||||
|
|
||||||
|
|
||||||
|
def solve(data):
|
||||||
|
offset = 10_000_000_000_000
|
||||||
|
machines = data.split("\n\n")
|
||||||
|
p1 = ratio(machines)
|
||||||
|
p2 = ratio(machines, offset)
|
||||||
|
return p1, p2
|
||||||
|
|
||||||
|
|
||||||
|
def ratio(machines, offset=0):
|
||||||
|
cost = 0
|
||||||
|
for mid, config in enumerate(machines):
|
||||||
|
a_x, a_y, b_x, b_y, goal_x, goal_y = ints(config)
|
||||||
|
|
||||||
|
goal_x, goal_y = goal_x + offset, goal_y + offset
|
||||||
|
|
||||||
|
ratio = (goal_x * a_y - goal_y * a_x) / (goal_y * b_x - goal_x * b_y)
|
||||||
|
a_presses = round(goal_x / (b_x * ratio + a_x))
|
||||||
|
b_presses = round(a_presses * ratio)
|
||||||
|
|
||||||
|
a_presses, b_presses = int(a_presses), int(b_presses)
|
||||||
|
if (a_x + a_y) * a_presses + (b_x + b_y) * b_presses == goal_x + goal_y:
|
||||||
|
cost += 3 * a_presses + b_presses
|
||||||
|
return cost
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
with open("./input/13.txt", "r") as f:
|
||||||
|
inp = f.read().strip()
|
||||||
|
|
||||||
|
p1, p2 = solve(inp)
|
||||||
|
|
||||||
|
print(p1)
|
||||||
|
print(p2)
|
||||||
55
2024-python/output/day_14.py
Normal file
55
2024-python/output/day_14.py
Normal file
|
|
@ -0,0 +1,55 @@
|
||||||
|
from output import sints, vdbg
|
||||||
|
|
||||||
|
|
||||||
|
def solve(data, find_easter_egg=False, display_easter_egg=True):
|
||||||
|
W = 101
|
||||||
|
H = 103
|
||||||
|
easter_egg_recurring = 103
|
||||||
|
easter_egg_offset = 64
|
||||||
|
robots = [sints(line) for line in data.splitlines()]
|
||||||
|
state = {}
|
||||||
|
for id, robot in enumerate(robots):
|
||||||
|
x, y, *_ = robot
|
||||||
|
state[id] = (x, y)
|
||||||
|
i = 0
|
||||||
|
easter_egg_appearance = 7892
|
||||||
|
while i < easter_egg_appearance:
|
||||||
|
if i == 100:
|
||||||
|
midW = W // 2
|
||||||
|
midH = H // 2
|
||||||
|
safety_factor_t100 = (
|
||||||
|
sum(0 <= x < midW and 0 <= y < midH for x, y in state.values())
|
||||||
|
* sum(midW < x < W and 0 <= y < midH for x, y in state.values())
|
||||||
|
* sum(0 <= x < midW and midH < y < H for x, y in state.values())
|
||||||
|
* sum(midW < x < W and midH < y < H for x, y in state.values())
|
||||||
|
)
|
||||||
|
for id, props in enumerate(robots):
|
||||||
|
_ix, _iy, vx, vy = props
|
||||||
|
x, y = state[id]
|
||||||
|
state[id] = ((x + vx) % W, (y + vy) % H)
|
||||||
|
i += 1
|
||||||
|
if find_easter_egg:
|
||||||
|
if i % easter_egg_recurring == easter_egg_offset:
|
||||||
|
vdbg(state.values(), H, W)
|
||||||
|
print(i)
|
||||||
|
input("--- any key pls ---")
|
||||||
|
if display_easter_egg:
|
||||||
|
easter_egg = [
|
||||||
|
(y - 26, x - 23)
|
||||||
|
for x, y in state.values()
|
||||||
|
if 20 <= x <= 53 and 24 <= y <= 60
|
||||||
|
]
|
||||||
|
max_y, max_x = max(easter_egg)
|
||||||
|
vdbg(easter_egg, max_y + 1, max_x + 1)
|
||||||
|
print("")
|
||||||
|
return safety_factor_t100, easter_egg_appearance
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
with open("./input/14.txt", "r") as f:
|
||||||
|
inp = f.read().strip()
|
||||||
|
|
||||||
|
p1, p2 = solve(inp, display_easter_egg=False)
|
||||||
|
|
||||||
|
print(p1)
|
||||||
|
print(p2)
|
||||||
115
2024-python/output/day_15.py
Normal file
115
2024-python/output/day_15.py
Normal file
|
|
@ -0,0 +1,115 @@
|
||||||
|
from output import D, matrix
|
||||||
|
|
||||||
|
|
||||||
|
def solve(data):
|
||||||
|
O = {k: v for k, v in zip("^>v<", D)}
|
||||||
|
grid, movements = data.split("\n\n")
|
||||||
|
grid, H, W = matrix(grid)
|
||||||
|
movements = "".join(movements.split())
|
||||||
|
|
||||||
|
grid_1 = {
|
||||||
|
(r, c): value for r, row in enumerate(grid) for c, value in enumerate(row)
|
||||||
|
}
|
||||||
|
|
||||||
|
grid_2 = {}
|
||||||
|
for pos, value in grid_1.items():
|
||||||
|
r2, c2 = pos
|
||||||
|
match value:
|
||||||
|
case "O":
|
||||||
|
grid_2[(r2, c2 * 2)] = "["
|
||||||
|
grid_2[(r2, c2 * 2 + 1)] = "]"
|
||||||
|
case "@":
|
||||||
|
grid_2[(r2, c2 * 2)] = "@"
|
||||||
|
grid_2[(r2, c2 * 2 + 1)] = "."
|
||||||
|
case _:
|
||||||
|
grid_2[(r2, c2 * 2)] = value
|
||||||
|
grid_2[(r2, c2 * 2 + 1)] = value
|
||||||
|
|
||||||
|
answers = []
|
||||||
|
|
||||||
|
for grid, W in [(grid_1, W), (grid_2, W * 2)]:
|
||||||
|
pos = next((r, c) for r in range(H) for c in range(W) if grid[(r, c)] == "@")
|
||||||
|
|
||||||
|
for move in movements:
|
||||||
|
r, c = pos
|
||||||
|
dr, dc = O[move]
|
||||||
|
move_boxes = True
|
||||||
|
blocked = False
|
||||||
|
|
||||||
|
if (r + dr, c + dc) not in grid:
|
||||||
|
continue
|
||||||
|
|
||||||
|
match grid[(r + dr, c + dc)]:
|
||||||
|
case ".":
|
||||||
|
move_boxes = False
|
||||||
|
case "#":
|
||||||
|
move_boxes = False
|
||||||
|
continue
|
||||||
|
|
||||||
|
if move_boxes:
|
||||||
|
seen = set()
|
||||||
|
|
||||||
|
Q = [(r + dr, c + dc)]
|
||||||
|
|
||||||
|
while Q:
|
||||||
|
rr, cc = Q.pop(0)
|
||||||
|
|
||||||
|
if (rr, cc) in seen:
|
||||||
|
continue
|
||||||
|
|
||||||
|
if (rr, cc) not in grid:
|
||||||
|
continue
|
||||||
|
|
||||||
|
if grid[(rr, cc)] == "#":
|
||||||
|
blocked = True
|
||||||
|
continue
|
||||||
|
|
||||||
|
seen.add((rr, cc))
|
||||||
|
|
||||||
|
if grid[(rr, cc)] in ".":
|
||||||
|
continue
|
||||||
|
|
||||||
|
Q.append((rr + dr, cc + dc))
|
||||||
|
if dr != 0:
|
||||||
|
match grid[(rr, cc)]:
|
||||||
|
case "]":
|
||||||
|
Q.append((rr, cc - 1))
|
||||||
|
case "[":
|
||||||
|
Q.append((rr, cc + 1))
|
||||||
|
|
||||||
|
if blocked:
|
||||||
|
continue
|
||||||
|
|
||||||
|
patchset = {
|
||||||
|
(rr, cc): (rr + dr, cc + dc)
|
||||||
|
for rr, cc in seen
|
||||||
|
if grid[(rr, cc)] != "."
|
||||||
|
}
|
||||||
|
|
||||||
|
dotset = set(seen) - set(patchset.values())
|
||||||
|
|
||||||
|
for oldpos, newpos in sorted(patchset.items())[:: -(dr + dc)]:
|
||||||
|
value = grid[oldpos]
|
||||||
|
grid[newpos] = value
|
||||||
|
|
||||||
|
for dot in dotset:
|
||||||
|
grid[dot] = "."
|
||||||
|
|
||||||
|
grid[(r, c)] = "."
|
||||||
|
grid[(r + dr, c + dc)] = "@"
|
||||||
|
pos = r + dr, c + dc
|
||||||
|
|
||||||
|
answers.append(sum(k[0] * 100 + k[1] for k, v in grid.items() if v in "O["))
|
||||||
|
|
||||||
|
p1, p2 = answers
|
||||||
|
return p1, p2
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
with open("./input/15.txt", "r") as f:
|
||||||
|
inp = f.read().strip()
|
||||||
|
|
||||||
|
p1, p2 = solve(inp)
|
||||||
|
|
||||||
|
print(p1)
|
||||||
|
print(p2)
|
||||||
78
2024-python/output/day_16.py
Normal file
78
2024-python/output/day_16.py
Normal file
|
|
@ -0,0 +1,78 @@
|
||||||
|
from heapq import heappop, heappush
|
||||||
|
|
||||||
|
from output import D, matrix
|
||||||
|
|
||||||
|
|
||||||
|
def solve(data):
|
||||||
|
grid, H, W = matrix(data)
|
||||||
|
S = [(r, c) for r in range(H) for c in range(W) if grid[r][c] == "S"][0]
|
||||||
|
E = [(r, c) for r in range(H) for c in range(W) if grid[r][c] == "E"][0]
|
||||||
|
Q = [(0, S, 1)]
|
||||||
|
p1 = float("inf")
|
||||||
|
SE = dict()
|
||||||
|
ES = dict()
|
||||||
|
seen = set()
|
||||||
|
while Q:
|
||||||
|
cost, pos, dir = heappop(Q)
|
||||||
|
r, c = pos
|
||||||
|
if grid[r][c] == "#":
|
||||||
|
continue
|
||||||
|
if (r, c, dir) in seen:
|
||||||
|
continue
|
||||||
|
seen.add((r, c, dir))
|
||||||
|
if (r, c, dir) not in SE:
|
||||||
|
SE[(r, c, dir)] = cost
|
||||||
|
if pos == E:
|
||||||
|
p1 = min(p1, cost)
|
||||||
|
continue
|
||||||
|
for inc, delta, facing in [
|
||||||
|
(1, D[dir], dir),
|
||||||
|
(1000, (0, 0), (dir + 1) % 4),
|
||||||
|
(1000, (0, 0), (dir - 1) % 4),
|
||||||
|
]:
|
||||||
|
nc = cost + inc
|
||||||
|
dr, dc = delta
|
||||||
|
heappush(Q, (nc, (r + dr, c + dc), facing))
|
||||||
|
Q = [(0, E, 0), (0, E, 1), (0, E, 2), (0, E, 3)]
|
||||||
|
seen = set()
|
||||||
|
while Q:
|
||||||
|
cost, pos, dir = heappop(Q)
|
||||||
|
r, c = pos
|
||||||
|
if grid[r][c] == "#":
|
||||||
|
continue
|
||||||
|
if (r, c, dir) in seen:
|
||||||
|
continue
|
||||||
|
seen.add((r, c, dir))
|
||||||
|
if (r, c, (dir + 2) % 4) not in ES:
|
||||||
|
ES[(r, c, (dir + 2) % 4)] = cost
|
||||||
|
if pos == S:
|
||||||
|
continue
|
||||||
|
for inc, delta, facing in [
|
||||||
|
(1, D[dir], dir),
|
||||||
|
(1000, (0, 0), (dir + 1) % 4),
|
||||||
|
(1000, (0, 0), (dir - 1) % 4),
|
||||||
|
]:
|
||||||
|
nc = cost + inc
|
||||||
|
dr, dc = delta
|
||||||
|
heappush(Q, (nc, (r + dr, c + dc), facing))
|
||||||
|
p2 = set()
|
||||||
|
for r in range(H):
|
||||||
|
for c in range(W):
|
||||||
|
for d in range(4):
|
||||||
|
if (
|
||||||
|
(r, c, d) in SE
|
||||||
|
and (r, c, d) in ES
|
||||||
|
and SE[(r, c, d)] + ES[(r, c, d)] == p1
|
||||||
|
):
|
||||||
|
p2.add((r, c))
|
||||||
|
return p1, len(p2)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
with open("./input/16.txt", "r") as f:
|
||||||
|
inp = f.read().strip()
|
||||||
|
|
||||||
|
p1, p2 = solve(inp)
|
||||||
|
|
||||||
|
print(p1)
|
||||||
|
print(p2)
|
||||||
75
2024-python/output/day_17.py
Normal file
75
2024-python/output/day_17.py
Normal file
|
|
@ -0,0 +1,75 @@
|
||||||
|
from collections import deque
|
||||||
|
|
||||||
|
from output import ints
|
||||||
|
|
||||||
|
|
||||||
|
def solve(data):
|
||||||
|
R, program = data.split("\n\n")
|
||||||
|
R = {k: int(v) for k, v in zip("abc", ints(R))}
|
||||||
|
program = ints(program)
|
||||||
|
P = ",".join(map(str, program))
|
||||||
|
|
||||||
|
p1 = org_version(R["a"], program)
|
||||||
|
p2 = float("inf")
|
||||||
|
|
||||||
|
Q = deque([1])
|
||||||
|
while Q:
|
||||||
|
x = Q.popleft()
|
||||||
|
for i in range(8):
|
||||||
|
o = org_version(x + i, program)
|
||||||
|
if P.endswith(o):
|
||||||
|
if o == P:
|
||||||
|
p2 = min(p2, x + i)
|
||||||
|
else:
|
||||||
|
Q.append((x + i) * 8)
|
||||||
|
|
||||||
|
return p1, p2
|
||||||
|
|
||||||
|
|
||||||
|
def org_version(A, program):
|
||||||
|
R = {"a": A, "b": 0, "c": 0}
|
||||||
|
|
||||||
|
def combo(v):
|
||||||
|
return v if v < 4 else R[chr(93 + v)]
|
||||||
|
|
||||||
|
i = 0
|
||||||
|
out = []
|
||||||
|
while i < len(program):
|
||||||
|
cl = program[i + 1]
|
||||||
|
match program[i]:
|
||||||
|
case 0:
|
||||||
|
R["a"] = R["a"] // (2 ** combo(cl))
|
||||||
|
i += 2
|
||||||
|
case 1:
|
||||||
|
a, b = R["b"], cl
|
||||||
|
R["b"] = a ^ b
|
||||||
|
i += 2
|
||||||
|
case 2:
|
||||||
|
R["b"] = combo(cl) % 8
|
||||||
|
i += 2
|
||||||
|
case 3:
|
||||||
|
i = (i + 2) if R["a"] == 0 else cl
|
||||||
|
case 4:
|
||||||
|
a, b = R["b"], R["c"]
|
||||||
|
R["b"] = a ^ b
|
||||||
|
i += 2
|
||||||
|
case 5:
|
||||||
|
out.append(combo(cl) % 8)
|
||||||
|
i += 2
|
||||||
|
case 6:
|
||||||
|
R["b"] = R["a"] // (2 ** combo(cl))
|
||||||
|
i += 2
|
||||||
|
case 7:
|
||||||
|
R["c"] = R["a"] // (2 ** combo(cl))
|
||||||
|
i += 2
|
||||||
|
return ",".join(map(str, out))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
with open("./input/17.txt", "r") as f:
|
||||||
|
inp = f.read().strip()
|
||||||
|
|
||||||
|
p1, p2 = solve(inp)
|
||||||
|
|
||||||
|
print(p1)
|
||||||
|
print(p2)
|
||||||
90
2024-python/output/day_18.py
Normal file
90
2024-python/output/day_18.py
Normal file
|
|
@ -0,0 +1,90 @@
|
||||||
|
from collections import deque
|
||||||
|
|
||||||
|
from output import ints, D
|
||||||
|
|
||||||
|
|
||||||
|
def solve(data):
|
||||||
|
grid = [tuple(ints(line)[::-1]) for line in data.splitlines()]
|
||||||
|
sample_size = 1024
|
||||||
|
H = 71
|
||||||
|
W = 71
|
||||||
|
E = H - 1, W - 1
|
||||||
|
p1 = None
|
||||||
|
p2 = None
|
||||||
|
FIRST_BLOCKED = 2974
|
||||||
|
for sample_size in [1024, FIRST_BLOCKED]:
|
||||||
|
answer = None
|
||||||
|
Q = deque([(0, 0, 0)])
|
||||||
|
seen = set()
|
||||||
|
while Q:
|
||||||
|
y, x, w = Q.popleft()
|
||||||
|
if (y, x) in seen:
|
||||||
|
continue
|
||||||
|
seen.add((y, x))
|
||||||
|
if (y, x) == E:
|
||||||
|
answer = w
|
||||||
|
for dy, dx in D:
|
||||||
|
if not (0 <= y + dy < H and 0 <= x + dx < W):
|
||||||
|
continue
|
||||||
|
if (y + dy, x + dx) in grid[:sample_size]:
|
||||||
|
continue
|
||||||
|
Q.append((y + dy, x + dx, w + 1))
|
||||||
|
if not answer:
|
||||||
|
r, c = grid[sample_size - 1]
|
||||||
|
p2 = f"{c},{r}"
|
||||||
|
else:
|
||||||
|
p1 = answer
|
||||||
|
return p1, p2
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
import os
|
||||||
|
|
||||||
|
# use dummy data
|
||||||
|
inp = """
|
||||||
|
5,4
|
||||||
|
4,2
|
||||||
|
4,5
|
||||||
|
3,0
|
||||||
|
2,1
|
||||||
|
6,3
|
||||||
|
2,4
|
||||||
|
1,5
|
||||||
|
0,6
|
||||||
|
3,3
|
||||||
|
2,6
|
||||||
|
5,1
|
||||||
|
1,2
|
||||||
|
5,5
|
||||||
|
2,5
|
||||||
|
6,5
|
||||||
|
1,4
|
||||||
|
0,4
|
||||||
|
6,4
|
||||||
|
1,1
|
||||||
|
6,1
|
||||||
|
1,0
|
||||||
|
0,5
|
||||||
|
1,6
|
||||||
|
2,0
|
||||||
|
""".strip()
|
||||||
|
|
||||||
|
# uncomment to instead use stdin
|
||||||
|
# import sys; inp = sys.stdin.read().strip()
|
||||||
|
|
||||||
|
# uncomment to use AoC provided puzzle input
|
||||||
|
with open("./input/18.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
|
||||||
39
2024-python/output/day_19.py
Normal file
39
2024-python/output/day_19.py
Normal file
|
|
@ -0,0 +1,39 @@
|
||||||
|
from functools import cache
|
||||||
|
from collections import defaultdict
|
||||||
|
|
||||||
|
|
||||||
|
def solve(data):
|
||||||
|
patterns, designs = data.split("\n\n")
|
||||||
|
patterns = sorted(patterns.split(", "), key=lambda s: -len(s))
|
||||||
|
designs = designs.split()
|
||||||
|
|
||||||
|
hashed = defaultdict(list)
|
||||||
|
for p in patterns:
|
||||||
|
hashed[p[0]].append(p)
|
||||||
|
hashed = tuple([(k, tuple(v)) for k, v in hashed.items()])
|
||||||
|
|
||||||
|
possible = sum(ispossible(d, hashed) > 0 for d in designs)
|
||||||
|
every_possibility = sum(ispossible(d, hashed) for d in designs)
|
||||||
|
return possible, every_possibility
|
||||||
|
|
||||||
|
|
||||||
|
@cache
|
||||||
|
def ispossible(remaining, patterns):
|
||||||
|
if not remaining:
|
||||||
|
return 1
|
||||||
|
selected = dict(patterns).get(remaining[0], [])
|
||||||
|
return sum(
|
||||||
|
ispossible(remaining.removeprefix(pattern), patterns)
|
||||||
|
for pattern in selected
|
||||||
|
if remaining.startswith(pattern)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
with open("./input/19.txt", "r") as f:
|
||||||
|
inp = f.read().strip()
|
||||||
|
|
||||||
|
p1, p2 = solve(inp)
|
||||||
|
|
||||||
|
print(p1)
|
||||||
|
print(p2)
|
||||||
58
2024-python/output/day_20.py
Normal file
58
2024-python/output/day_20.py
Normal file
|
|
@ -0,0 +1,58 @@
|
||||||
|
from collections import deque
|
||||||
|
|
||||||
|
from output import matrix, D, mhd
|
||||||
|
|
||||||
|
|
||||||
|
def solve(data):
|
||||||
|
grid, H, W = matrix(data)
|
||||||
|
S = next((r, c) for r in range(H) for c in range(W) if grid[r][c] == "S")
|
||||||
|
E = next((r, c) for r in range(H) for c in range(W) if grid[r][c] == "E")
|
||||||
|
seen = set()
|
||||||
|
Q = deque([(S, 0, [])])
|
||||||
|
while Q:
|
||||||
|
pos, w, path = Q.pop()
|
||||||
|
r, c = pos
|
||||||
|
if grid[r][c] == "#" or pos in seen:
|
||||||
|
continue
|
||||||
|
seen.add(pos)
|
||||||
|
if pos == E:
|
||||||
|
og = path + [(r, c)]
|
||||||
|
break
|
||||||
|
for dr, dc in D:
|
||||||
|
Q.append(((r + dr, c + dc), w + 1, path + [(r, c)]))
|
||||||
|
|
||||||
|
track = {rc: i for i, rc in enumerate(og)}
|
||||||
|
T = 100
|
||||||
|
p1 = 0
|
||||||
|
p2 = 0
|
||||||
|
|
||||||
|
cheats = set()
|
||||||
|
for r, c in track.keys():
|
||||||
|
for dr, dc in D:
|
||||||
|
rr, cc = r + dr * 2, c + dc * 2
|
||||||
|
if (
|
||||||
|
(rr, cc) not in cheats
|
||||||
|
and (rr, cc) in track
|
||||||
|
and T + 2 <= abs(track[(rr, cc)] - track[(r, c)])
|
||||||
|
):
|
||||||
|
p1 += 1
|
||||||
|
cheats.add((r, c))
|
||||||
|
|
||||||
|
tk = [k for k in track.keys()]
|
||||||
|
for b in range(T, len(tk)):
|
||||||
|
for a in range(b - T):
|
||||||
|
distance = mhd(tk[a], tk[b])
|
||||||
|
if distance <= 20 and b - a - distance >= T:
|
||||||
|
p2 += 1
|
||||||
|
|
||||||
|
return p1, p2
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
with open("./input/20.txt", "r") as f:
|
||||||
|
inp = f.read().strip()
|
||||||
|
|
||||||
|
p1, p2 = solve(inp)
|
||||||
|
|
||||||
|
print(p1)
|
||||||
|
print(p2)
|
||||||
67
2024-python/output/day_21.py
Normal file
67
2024-python/output/day_21.py
Normal file
|
|
@ -0,0 +1,67 @@
|
||||||
|
from functools import cache
|
||||||
|
|
||||||
|
DKP = {
|
||||||
|
"^": (0, 1),
|
||||||
|
"A": (0, 2),
|
||||||
|
"<": (1, 0),
|
||||||
|
"v": (1, 1),
|
||||||
|
">": (1, 2),
|
||||||
|
}
|
||||||
|
|
||||||
|
NKP = {
|
||||||
|
"7": (0, 0),
|
||||||
|
"8": (0, 1),
|
||||||
|
"9": (0, 2),
|
||||||
|
"4": (1, 0),
|
||||||
|
"5": (1, 1),
|
||||||
|
"6": (1, 2),
|
||||||
|
"1": (2, 0),
|
||||||
|
"2": (2, 1),
|
||||||
|
"3": (2, 2),
|
||||||
|
"0": (3, 1),
|
||||||
|
"A": (3, 2),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def solve(data):
|
||||||
|
codes = data.split()
|
||||||
|
p1 = 0
|
||||||
|
p2 = 0
|
||||||
|
for code in codes:
|
||||||
|
num = int(code[:-1])
|
||||||
|
p1 += num * unfold(code, 1 + 2, num=True)
|
||||||
|
p2 += num * unfold(code, 1 + 25, num=True)
|
||||||
|
return p1, p2
|
||||||
|
|
||||||
|
|
||||||
|
@cache
|
||||||
|
def unfold(sequence, iterations, num=False):
|
||||||
|
if iterations == 0:
|
||||||
|
return len(sequence)
|
||||||
|
s = "A"
|
||||||
|
total = 0
|
||||||
|
pad, invalid = (NKP, (3, 0)) if num else (DKP, (0, 0))
|
||||||
|
for d in sequence:
|
||||||
|
total += unfold(seq(pad[s], pad[d], invalid), iterations - 1)
|
||||||
|
s = d
|
||||||
|
return total
|
||||||
|
|
||||||
|
|
||||||
|
@cache
|
||||||
|
def seq(S, E, invalid):
|
||||||
|
y1, x1 = S
|
||||||
|
y2, x2 = E
|
||||||
|
seq = "<" * (x1 - x2) + "v" * (y2 - y1) + "^" * (y1 - y2) + ">" * (x2 - x1)
|
||||||
|
if (y2, x1) == invalid or (y1, x2) == invalid:
|
||||||
|
seq = seq[::-1]
|
||||||
|
return seq + "A"
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
with open("./input/21.txt", "r") as f:
|
||||||
|
inp = f.read().strip()
|
||||||
|
|
||||||
|
p1, p2 = solve(inp)
|
||||||
|
|
||||||
|
print(p1)
|
||||||
|
print(p2)
|
||||||
44
2024-python/output/day_22.py
Normal file
44
2024-python/output/day_22.py
Normal file
|
|
@ -0,0 +1,44 @@
|
||||||
|
import math
|
||||||
|
from collections import deque, defaultdict
|
||||||
|
|
||||||
|
from output import ints
|
||||||
|
|
||||||
|
|
||||||
|
def solve(data):
|
||||||
|
s = [(n, n % 10) for n in ints(data)]
|
||||||
|
bananas = defaultdict(int)
|
||||||
|
sum_2000th_secrets = 0
|
||||||
|
for j, n in enumerate(s):
|
||||||
|
seen = set()
|
||||||
|
L = deque()
|
||||||
|
for i in range(2000):
|
||||||
|
prize, dff = calc(*n)
|
||||||
|
if len(L) == 4:
|
||||||
|
L.popleft()
|
||||||
|
L.append(dff)
|
||||||
|
if len(L) == 4:
|
||||||
|
if tuple(L) not in seen:
|
||||||
|
seen.add(tuple(L))
|
||||||
|
bananas[tuple(L)] += prize % 10
|
||||||
|
n = prize, dff
|
||||||
|
sum_2000th_secrets += prize
|
||||||
|
max_bananas = max(bananas.values())
|
||||||
|
return sum_2000th_secrets, max_bananas
|
||||||
|
|
||||||
|
|
||||||
|
def calc(i, d=0):
|
||||||
|
a = i % 10
|
||||||
|
i = ((i * 64) ^ i) % 16777216
|
||||||
|
i = (math.floor(i / 32) ^ i) % 16777216
|
||||||
|
i = ((i * 2048) ^ i) % 16777216
|
||||||
|
return i, (i % 10) - a
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
with open("./input/22.txt", "r") as f:
|
||||||
|
inp = f.read().strip()
|
||||||
|
|
||||||
|
p1, p2 = solve(inp)
|
||||||
|
|
||||||
|
print(p1)
|
||||||
|
print(p2)
|
||||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Reference in a new issue