diff --git a/.gitignore b/.gitignore index e7078d0..2cceedb 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ -*/__pycache__/* +**/__pycache__/* diff --git a/2024/day02.py b/2024/day02.py index b731e97..160cc9b 100644 --- a/2024/day02.py +++ b/2024/day02.py @@ -31,7 +31,7 @@ def taskB (data) -> int: gameScoreB = 0 for report in data: reportData = [int(data) for data in report.split(' ')] - gameScoreB = gameScoreB +any([isCorrect(reportData[:i] + reportData[i + 1:]) for i in range(len(reportData))]) + gameScoreB = gameScoreB + any([isCorrect(reportData[:i] + reportData[i + 1:]) for i in range(len(reportData))]) return gameScoreB def task(task: int,data) -> int: diff --git a/2024/day04.py b/2024/day04.py index d140fbf..4444f67 100644 --- a/2024/day04.py +++ b/2024/day04.py @@ -17,10 +17,34 @@ filename = getFilename2Data(aocDay) def checkNextChar(inputChar): return 0 -def countXMAS2(data): +def countXMAS3(data): #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 +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): diff --git a/2024/day04a.py b/2024/day04a.py new file mode 100644 index 0000000..0a3d976 --- /dev/null +++ b/2024/day04a.py @@ -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) \ No newline at end of file