Day 4 solves

This commit is contained in:
d4rkr41n
2024-12-04 11:00:25 -06:00
parent 8a1f21dba8
commit 48b8dc55b9
4 changed files with 319 additions and 0 deletions
+37
View File
@@ -0,0 +1,37 @@
with open("../input.txt", "r") as fp:
inputs = [line.rstrip() for line in fp]
nextChar = {
'X': 'M',
'M': 'A',
'A': 'S'
}
def checkWord(inputs, char, pos, dir):
# What to check, () pos, () direction traveling
py, px = pos
dy, dx = dir
if inputs[py][px] == char:
if char == 'S':
return True
py += dy
px += dx
if px < 0 or px >= len(inputs[0]):
return False
elif py < 0 or py >= len(inputs):
return False
return checkWord(inputs, nextChar.get(char), (py,px), (dy,dx))
return False
count = 0
for y,line in enumerate(inputs):
for x,char in enumerate(line):
for dy,dx in [(0,-1),(0,1),(-1,0),(1,0),(1,1),(-1,-1),(-1,1),(1,-1)]:
if checkWord(inputs,'X',(x,y),(dy,dx)):
count += 1
print(count)