Day 4 solves
This commit is contained in:
@@ -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)
|
||||
@@ -0,0 +1,30 @@
|
||||
with open("../input.txt", "r") as fp:
|
||||
inputs = [line.rstrip() for line in fp]
|
||||
|
||||
def checkWord(inputs, pos):
|
||||
py, px = pos
|
||||
|
||||
if py-1 < 0 or py+1 >= len(inputs):
|
||||
return 0
|
||||
if px-1 < 0 or px+1 >= len(inputs[0]):
|
||||
return 0
|
||||
|
||||
TL = inputs[py-1][px-1]
|
||||
BR = inputs[py+1][px+1]
|
||||
if TL+'A'+BR != "MAS" and TL+'A'+BR != "SAM":
|
||||
return 0
|
||||
|
||||
TR = inputs[py-1][px+1]
|
||||
BL = inputs[py+1][px-1]
|
||||
if TR+'A'+BL != "MAS" and TR+'A'+BL != "SAM":
|
||||
return 0
|
||||
|
||||
return 1
|
||||
|
||||
count = 0
|
||||
for y,line in enumerate(inputs):
|
||||
for x,char in enumerate(line):
|
||||
if char == 'A':
|
||||
count += checkWord(inputs,(y,x))
|
||||
|
||||
print(count)
|
||||
Reference in New Issue
Block a user