add: 2024/day04/part1
This commit is contained in:
parent
7be7d0654b
commit
b638d89a0a
32
2024/day04/part1.py
Normal file
32
2024/day04/part1.py
Normal file
@ -0,0 +1,32 @@
|
||||
import re
|
||||
|
||||
text: str
|
||||
|
||||
with open("input.txt") as f:
|
||||
text = f.read()
|
||||
|
||||
lines: list[str] = text.splitlines()
|
||||
|
||||
def search(values: list[list[str]], word: str, coord_x: int, coord_y: int, inc_x: int, inc_y: int):
|
||||
|
||||
array_length = len(values)
|
||||
line_length = len(values[0])
|
||||
|
||||
for i in range(len(word)):
|
||||
x: int = coord_x + i * inc_x
|
||||
y: int = coord_y + i * inc_y
|
||||
if (x >= line_length or x < 0) or (y >= array_length or y < 0) or (values[y][x] != word[i]):
|
||||
return 0
|
||||
return 1
|
||||
|
||||
total: int = 0
|
||||
|
||||
directions = [(-1, -1), (0, -1), (+1, -1), (+1, 0), (+1, +1), (0, +1), (-1, +1), (-1, 0)]
|
||||
|
||||
for y, line in enumerate(lines):
|
||||
for x, c in enumerate(line):
|
||||
if (c == 'X'):
|
||||
for inc_x, inc_y in directions:
|
||||
total += search(lines, "XMAS", x, y, inc_x, inc_y)
|
||||
|
||||
print(total)
|
Loading…
Reference in New Issue
Block a user