advent24-llm / day04 /solution_gemini-1.5-pro.py
jerpint's picture
Add solution files
a4da721
def solve():
with open("./input.txt", "r") as f:
grid = [line.strip() for line in f]
rows = len(grid)
cols = len(grid[0])
def check_pattern(r, c, pattern):
for dr in [-1, 0, 1]:
for dc in [-1, 0, 1]:
if dr == 0 and dc == 0:
continue
found = True
for i in range(len(pattern)):
nr, nc = r + dr * i, c + dc * i
if 0 <= nr < rows and 0 <= nc < cols and grid[nr][nc] == pattern[i]:
continue
else:
found = False
break
if found:
return True
return False
# Part 1
count1 = 0
for r in range(rows):
for c in range(cols):
if check_pattern(r, c, "XMAS") or check_pattern(r, c, "SAMX"):
count1 += 1
print(count1)
# Part 2
count2 = 0
for r in range(rows):
for c in range(cols):
if (
1 <= r < rows - 1 and 1 <= c < cols - 1 and
(grid[r-1][c-1] == 'M' or grid[r-1][c-1] == 'S') and
(grid[r-1][c+1] == 'M' or grid[r-1][c+1] == 'S') and
(grid[r+1][c-1] == 'M' or grid[r+1][c-1] == 'S') and
(grid[r+1][c+1] == 'M' or grid[r+1][c+1] == 'S') and
(grid[r-1][c] == 'A' or grid[r-1][c] == 'A') and
(grid[r][c-1] == 'A' or grid[r][c-1] == 'A') and
(grid[r][c+1] == 'A' or grid[r][c+1] == 'A') and
(grid[r+1][c] == 'A' or grid[r+1][c] == 'A') and
(grid[r][c] == 'S' or grid[r][c] == 'M')
):
count2 += 1
print(count2)
solve()