Skip to content

Commit 4b44b4c

Browse files
committed
feat: 2024 day 20
1 parent 5b341d3 commit 4b44b4c

File tree

3 files changed

+133
-0
lines changed

3 files changed

+133
-0
lines changed

2024/20a.py

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
from aoc import *
2+
import sys
3+
4+
5+
DIRS = ((-1, 0), (0, 1), (1, 0), (0, -1))
6+
7+
8+
def search(i, j, m, lengths, length):
9+
"""
10+
:param lengths: lengths from the end
11+
"""
12+
togo = []
13+
for d in DIRS:
14+
pos = i + d[0], j + d[1]
15+
if (
16+
pos in m
17+
and m[pos] in '.SE'
18+
and (pos not in lengths or lengths[pos] > length + 1)
19+
):
20+
lengths[pos] = length + 1
21+
togo.append(pos)
22+
for pos in togo:
23+
search(*pos, m, lengths, length + 1)
24+
25+
26+
def main(infi: str):
27+
sys.setrecursionlimit(10000)
28+
inp = load_map_dd(infi)
29+
end = [(i, j) for (i, j), e in inp.items() if e == 'E'][0]
30+
lengths = {}
31+
search(*end, inp, lengths, 0)
32+
s = 0
33+
for (i, j), e in inp.items():
34+
if e == '#':
35+
connected = [
36+
(i + d[0], j + d[1])
37+
for d in DIRS
38+
if (i + d[0], j + d[1]) in inp
39+
and inp[i + d[0], j + d[1]] in '.SE'
40+
]
41+
if len(connected) == 2:
42+
length = abs(lengths[connected[0]] - lengths[connected[1]]) - 2
43+
if length >= 100:
44+
s += 1
45+
elif len(connected) > 2:
46+
continue
47+
return s
48+
49+
50+
DAY = 20
51+
FILE_TEST = f"{DAY}_testa.txt"
52+
# FILE_TEST = f"{DAY}_testb.txt"
53+
FILE_EXP = f"{DAY}_exp.txt"
54+
FILE = f"{DAY}.txt"
55+
# test_and_submit(main, FILE_TEST, FILE_EXP, FILE, DAY)
56+
# print(main(FILE_TEST))
57+
print(main(FILE))

2024/20b.py

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
from aoc import *
2+
import sys
3+
4+
5+
DIRS = ((-1, 0), (0, 1), (1, 0), (0, -1))
6+
7+
8+
def search(i, j, m, lengths, length):
9+
"""
10+
:param lengths: lengths from the end
11+
"""
12+
togo = []
13+
for d in DIRS:
14+
pos = i + d[0], j + d[1]
15+
if (
16+
pos in m
17+
and m[pos] in '.SE'
18+
and (pos not in lengths or lengths[pos] > length + 1)
19+
):
20+
lengths[pos] = length + 1
21+
togo.append(pos)
22+
for pos in togo:
23+
search(*pos, m, lengths, length + 1)
24+
25+
26+
def generate_distances():
27+
diffs = set()
28+
for i in range(2, 21):
29+
for a in range(i):
30+
b = i - a
31+
diffs.update(
32+
{
33+
(a, b),
34+
(a, -b),
35+
(-a, b),
36+
(-a, -b),
37+
(b, a),
38+
(b, -a),
39+
(-b, a),
40+
(-b, -a),
41+
}
42+
)
43+
return diffs
44+
45+
46+
def main(infi: str):
47+
sys.setrecursionlimit(10000)
48+
inp = load_map_dd(infi)
49+
end = [(i, j) for (i, j), e in inp.items() if e == 'E'][0]
50+
lengths = {}
51+
search(*end, inp, lengths, 0)
52+
distances = generate_distances()
53+
s = 0
54+
for (i, j), e in inp.items():
55+
if e in '.SE':
56+
for a, b in distances:
57+
if inp.get((i + a, j + b), None) in ['.', 'E', 'S']:
58+
length = (
59+
lengths[i + a, j + b]
60+
- lengths[i, j]
61+
- (abs(a) + abs(b))
62+
)
63+
if length >= 100:
64+
s += 1
65+
return s
66+
67+
68+
DAY = 20
69+
FILE_TEST = f"{DAY}_testa.txt"
70+
# FILE_TEST = f"{DAY}_testb.txt"
71+
FILE_EXP = f"{DAY}_exp.txt"
72+
FILE = f"{DAY}.txt"
73+
# test_and_submit(main, FILE_TEST, FILE_EXP, FILE, DAY)
74+
# print(main(FILE_TEST))
75+
print(main(FILE))

README.org

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
| 23 | 00:24:26 | 2632 | 0 | 01:09:24 | 2938 | 0 |
1010
| 22 | 00:07:06 | 766 | 0 | 05:23:29 | 7143 | 0 |
1111
| 21 | >24h | 15115 | 0 | >24h | 11364 | 0 |
12+
| 20 | 02:16:45 | 5713 | 0 | >24h | 20970 | 0 |
1213
| 19 | 01:44:22 | 6641 | 0 | 01:50:06 | 5444 | 0 |
1314
| 17 | 00:27:08 | 1748 | 0 | 07:06:10 | 5688 | 0 |
1415
| 15 | 00:32:17 | 2397 | 0 | 02:59:49 | 4379 | 0 |

0 commit comments

Comments
 (0)