-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
51 lines (39 loc) · 1.49 KB
/
main.py
File metadata and controls
51 lines (39 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# --- Day 4: Ceres Search ---
def part1():
# directions = [(0,1), (0,-1), (1,0), (1,1), (1,-1), (-1,0), (-1,-1), (-1,1)]
directions = [
(dy, dx) for dy in [-1, 0, 1] for dx in [-1, 0, 1] if (dx != 0 or dy != 0)
]
word = "XMAS"
result = 0
word_len = len(word)
lines = open("../../rust/data/inputs/04.txt", mode="r").readlines()
h, w = len(lines), len(lines[0]) - 1
grid = {(y, x): lines[y][x] for y in range(h) for x in range(w)}
for y, x in grid:
for dy, dx in directions:
candidate = "".join(
grid.get((y + dy * i, x + dx * i), "") for i in range(word_len)
)
result += candidate == word
return result
def part2():
lines = open("../../rust/data/inputs/04.txt", mode="r").readlines()
result = 0
h, w = len(lines), len(lines[0]) - 1
grid = {(y, x): lines[y][x] for y in range(h) for x in range(w)}
for i in range(0, h):
for j in range(0, w):
if grid[i, j] == "A" and i > 0 and j > 0 and i < w - 1 and j < h - 1:
c1 = grid[i - 1, j - 1]
c2 = grid[i + 1, j + 1]
c3 = grid[i - 1, j + 1]
c4 = grid[i + 1, j - 1]
if ((c1 == "M" and c2 == "S") or (c1 == "S" and c2 == "M")) and (
(c3 == "M" and c4 == "S") or (c3 == "S" and c4 == "M")
):
result += 1
return result
if __name__ == "__main__":
print(part1())
print(part2())