advent_of_code/2024/4/4.py
2024-12-06 12:43:53 +01:00

60 lines
1.5 KiB
Python

#!/usr/bin/env python
# 2024 - Advent Of Code 4
# file = 'input_example.txt'
file = 'input.txt'
# pylint: disable=consider-using-with
input_lines = [line.strip('\n') for line in open(file, encoding="utf-8")]
MAX_X = len(input_lines[0])
MAX_Y = len(input_lines)
# for line in input_lines:
# print(f'{line}')
def find_xmas(grid, start_pos, vector):
# vector = (x, y)
x = start_pos[0]
y = start_pos[1]
xmas = ['X', 'M', 'A', 'S']
for i, _ in enumerate(xmas):
if grid[y][x] == xmas[i]:
if i == 3:
return True
x += vector[0]
if x < 0 or x > MAX_X-1:
return False
y += vector[1]
if y < 0 or y > MAX_Y-1:
return False
else:
return False
return True
accum = 0
for pos_y in range(0, MAX_Y):
for pos_x in range(0, MAX_X):
if find_xmas(input_lines, (pos_x, pos_y), (1, 0)):
accum += 1
if find_xmas(input_lines, (pos_x, pos_y), (-1, 0)):
accum += 1
if find_xmas(input_lines, (pos_x, pos_y), (0, 1)):
accum += 1
if find_xmas(input_lines, (pos_x, pos_y), (0, -1)):
accum += 1
if find_xmas(input_lines, (pos_x, pos_y), (1, 1)):
accum += 1
if find_xmas(input_lines, (pos_x, pos_y), (-1, -1)):
accum += 1
if find_xmas(input_lines, (pos_x, pos_y), (1, -1)):
accum += 1
if find_xmas(input_lines, (pos_x, pos_y), (-1, 1)):
accum += 1
print(f'{accum=}')