Skip to content

Commit 2025525

Browse files
committed
Add solution to 2024-12-04
1 parent 59e58d7 commit 2025525

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

2024/day04/solutions.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from collections import defaultdict
2+
from itertools import product
3+
4+
with open("input") as f:
5+
ls = f.read().strip().split("\n")
6+
7+
8+
boardz = defaultdict(str)
9+
boardz |= {i + 1j * j: x for i, l in enumerate(ls) for j, x in enumerate(l)}
10+
octdir = {i + 1j * j for (i, j) in set(product((-1, 0, 1), (-1, 0, 1))) - {(0, 0)}}
11+
12+
# Part 1
13+
print(
14+
sum(
15+
[boardz[z + i * dz] for i in range(4)] == ["X", "M", "A", "S"]
16+
for z in list(boardz.keys())
17+
for dz in octdir
18+
)
19+
)
20+
21+
22+
# Part 2
23+
res = 0
24+
for z in list(boardz.keys()):
25+
if boardz[z] == "A":
26+
corners = [
27+
boardz[z + 1 + 1j],
28+
boardz[z + 1 - 1j],
29+
boardz[z - 1 - 1j],
30+
boardz[z - 1 + 1j],
31+
]
32+
if (
33+
corners.count("M") == 2
34+
and corners.count("S") == 2
35+
and boardz[z - 1 - 1j] != boardz[z + 1 + 1j]
36+
):
37+
res += 1
38+
print(res)

0 commit comments

Comments
 (0)