From d1e7ff9785230d8c548f0320a655fdadc138839c Mon Sep 17 00:00:00 2001 From: kleph Date: Thu, 5 Dec 2024 19:31:34 +0100 Subject: [PATCH] 2024 - Day 4 part 2 --- 2024/4/4_2.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 2024/4/4_2.py diff --git a/2024/4/4_2.py b/2024/4/4_2.py new file mode 100644 index 0000000..a4fb5bc --- /dev/null +++ b/2024/4/4_2.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python +# 2024 - Advent Of Code 4 - part 2 + +# 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): + x = start_pos[0] + y = start_pos[1] + + if grid[y - 1][x - 1] == 'M' and grid[y - 1][x + 1] == 'M': + if grid[y + 1][x - 1] == 'S' and grid[y + 1][x + 1] == 'S': + return True + + if grid[y - 1][x - 1] == 'M' and grid[y + 1][x - 1] == 'M': + if grid[y - 1][x + 1] == 'S' and grid[y + 1][x + 1] == 'S': + return True + + if grid[y - 1][x + 1] == 'M' and grid[y + 1][x + 1] == 'M': + if grid[y - 1][x - 1] == 'S' and grid[y + 1][x - 1] == 'S': + return True + + if grid[y + 1][x - 1] == 'M' and grid[y + 1][x + 1] == 'M': + if grid[y - 1][x - 1] == 'S' and grid[y - 1][x + 1] == 'S': + return True + + return False + +accum = 0 +for pos_y in range(1, MAX_Y - 1): + for pos_x in range(1, MAX_X - 1): + if input_lines[pos_y][pos_x] == 'A': + if find_xmas(input_lines, (pos_x, pos_y)): + accum += 1 + +print(f'{accum=}')