mirror of
https://github.com/thib8956/advent-of-code.git
synced 2024-12-25 13:26:29 +00:00
2024 day 4
This commit is contained in:
parent
9c9ac3bf3f
commit
0e030ea921
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,3 +1,6 @@
|
||||
# ignore aoc input file
|
||||
**/input.txt
|
||||
|
||||
# ---> Python
|
||||
# Byte-compiled / optimized / DLL files
|
||||
__pycache__/
|
||||
|
72
2024/day4/day4.py
Normal file
72
2024/day4/day4.py
Normal file
@ -0,0 +1,72 @@
|
||||
DIRECTIONS = (
|
||||
(0, -1), # N
|
||||
(-1, -1), # NW
|
||||
(1, -1), # NE
|
||||
(-1, 0), # W
|
||||
(1, 0), # E
|
||||
(0, 1), # S
|
||||
(-1, 1), # SW
|
||||
(1, 1), # SE
|
||||
)
|
||||
|
||||
def get_grid_pos(grid, pos):
|
||||
x, y = pos
|
||||
if 0 <= x < len(grid):
|
||||
if 0 <= y < len(grid[0]):
|
||||
return grid[y][x]
|
||||
return None
|
||||
|
||||
|
||||
def part1(grid):
|
||||
count = 0
|
||||
for y, row in enumerate(grid):
|
||||
for x, letter in enumerate(row):
|
||||
if letter != "X":
|
||||
continue
|
||||
for direction in DIRECTIONS:
|
||||
acc = letter
|
||||
for i in range(1, 4):
|
||||
dx, dy = direction
|
||||
pos = (x + i*dx, y + i*dy)
|
||||
next_letter = get_grid_pos(grid, pos)
|
||||
if next_letter is not None:
|
||||
acc += next_letter
|
||||
else:
|
||||
break # out-of-bounds, go to next direction
|
||||
if acc == "XMAS":
|
||||
count += 1
|
||||
print("Part 1: ", count)
|
||||
|
||||
|
||||
def part2(grid):
|
||||
count = 0
|
||||
for y, row in enumerate(grid):
|
||||
for x, _ in enumerate(row):
|
||||
if y + 2 >= len(grid) or x + 2 >= len(grid[0]):
|
||||
continue
|
||||
if grid[y+1][x+1] != "A": # center letter is always "A"
|
||||
continue
|
||||
# M.S / .A. / M.S
|
||||
if grid[y][x] == "M" and grid[y][x+2] == "S" and grid[y+2][x] == "M" and grid[y+2][x+2] == "S":
|
||||
count += 1
|
||||
# M.M / .A. / S.S
|
||||
if grid[y][x] == "M" and grid[y][x+2] == "M" and grid[y+2][x] == "S" and grid[y+2][x+2] == "S":
|
||||
count += 1
|
||||
# S.M / .A. / S.M
|
||||
if grid[y][x] == "S" and grid[y][x+2] == "M" and grid[y+2][x] == "S" and grid[y+2][x+2] == "M":
|
||||
count += 1
|
||||
# S.S / .A. / M.M
|
||||
if grid[y][x] == "S" and grid[y][x+2] == "S" and grid[y+2][x] == "M" and grid[y+2][x+2] == "M":
|
||||
count += 1
|
||||
print("Part 2: ", count)
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import sys
|
||||
infile = sys.argv[1] if 1 < len(sys.argv) else "example.txt"
|
||||
with open(infile) as f:
|
||||
grid = [l.rstrip() for l in f.readlines()]
|
||||
part1(grid)
|
||||
part2(grid)
|
||||
|
10
2024/day4/example.txt
Normal file
10
2024/day4/example.txt
Normal file
@ -0,0 +1,10 @@
|
||||
MMMSXXMASM
|
||||
MSAMXMSMSA
|
||||
AMXSXMAAMM
|
||||
MSAMASMSMX
|
||||
XMASAMXAMM
|
||||
XXAMMXXAMA
|
||||
SMSMSASXSS
|
||||
SAXAMASAAA
|
||||
MAMMMXMMMM
|
||||
MXMXAXMASX
|
Loading…
Reference in New Issue
Block a user