-
Notifications
You must be signed in to change notification settings - Fork 0
/
04_CeresSearch.py
48 lines (35 loc) · 1.16 KB
/
04_CeresSearch.py
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
from lib import *
inp = get_input()
# construct search pattern for P2 in form of:
# [[(UL, "M"), (DL, "M"), (DR, "S"), (UR, "S")], ...]
# where UL, DL, DR, UR stand for the relative position vector
P2 = [[(NEIGHBORS_DICT[d], x) for d, x in zip(["UL", "DL", "DR", "UR"], m)] for m in ["MMSS", "SMMS", "SSMM", "MSSM"]]
p1 = 0
p2 = 0
for x in range(len(inp[0])):
for y in range(len(inp)):
# --- part 1
for xo, yo in get_positions():
L = ""
xx, yy = x, y
while check_index_exists(inp, yy, xx):
L += inp[yy][xx]
if len(L) >= 4 or not "XMAS".startswith(L):
break
xx+=xo
yy+=yo
if L in ["XMAS"]:
p1 += 1
# --- part 2
if inp[y][x] == "A":
for comb in P2:
matches = True
for (xo, yo),s in comb:
xx,yy = x+xo, y+yo
if not check_index_exists(inp, yy,xx) or inp[yy][xx] != s:
matches = False
break
if matches:
p2 += 1
print(p1)
print(p2)