update 2024.12.04 14:54

This commit is contained in:
2024-12-04 14:54:52 +01:00
parent 0ca6d110e3
commit fbe12ad205
4 changed files with 71 additions and 3 deletions

2
.gitignore vendored
View File

@@ -1 +1 @@
*/__pycache__/* **/__pycache__/*

View File

@@ -17,10 +17,34 @@ filename = getFilename2Data(aocDay)
def checkNextChar(inputChar): def checkNextChar(inputChar):
return 0 return 0
def countXMAS2(data): def countXMAS3(data):
#ret = re2.findall("M.M\n.A.\nS.S", data, rotate=True) #ret = re2.findall("M.M\n.A.\nS.S", data, rotate=True)
for i in range(n):
for j in range(n):
if word_search[i][j] != 'A':
continue
if not is_inbounds(i+1, j+1):
continue
if not is_inbounds(i+1, j-1):
continue
if not is_inbounds(i-1, j+1):
continue
if not is_inbounds(i-1, j-1):
continue
if not (word_search[i-1][j-1], word_search[i+1][j+1]) in (('M', 'S'), ('S', 'M')):
continue
if not (word_search[i+1][j-1], word_search[i-1][j+1]) in (('M', 'S'), ('S', 'M')):
continue
result += 1
return 0 return 0
def countXMAS2(dataMatrix,searchWord):
for x in range(len(dataMatrix)):
for y in range(len(dataMatrix[x])):
if dataMatrix[x][y] == searchWord[1:2]:
def countXMAS(dataMatrix,searchWord): def countXMAS(dataMatrix,searchWord):

44
2024/day04a.py Normal file
View File

@@ -0,0 +1,44 @@
word_search = []
with open('/home/bandeira/git.bandeira/aoc/2024/day04/input.day04', 'r') as f:
for line in f.readlines():
word_search.append(list(line.strip()))
n = len(word_search)
def is_inbounds(i, j):
return 0 <= i < n and 0 <= j < n
# part 1
result = 0
for i in range(n):
for j in range(n):
if word_search[i][j] != 'X':
continue
for di in [-1, 0, 1]:
for dj in [-1, 0, 1]:
if (di, dj) == (0, 0):
continue
if is_inbounds(i+3*di, j+3*dj):
if ''.join(word_search[i+k*di][j+k*dj] for k in range(4)) == 'XMAS':
result += 1
print(result)
# part 2
result = 0
for i in range(n):
for j in range(n):
if word_search[i][j] != 'A':
continue
if not is_inbounds(i+1, j+1):
continue
if not is_inbounds(i+1, j-1):
continue
if not is_inbounds(i-1, j+1):
continue
if not is_inbounds(i-1, j-1):
continue
if not (word_search[i-1][j-1], word_search[i+1][j+1]) in (('M', 'S'), ('S', 'M')):
continue
if not (word_search[i+1][j-1], word_search[i-1][j+1]) in (('M', 'S'), ('S', 'M')):
continue
result += 1
print(result)