AdventOfCode/2024/04/solution.py
2024-12-04 21:02:55 +01:00

67 lines
2.2 KiB
Python

#!/bin/python3
import sys,re
from itertools import cycle
from pprint import pprint
sys.path.insert(0, '../../')
from fred import list2int,get_re,nprint,lprint, toGrid,grid_valid,addTuples,get_value_in_direction
input_f = 'input'
part = 2
grid = toGrid(input_f)
directions = {
'up': (-1, 0),
'down': (1, 0),
'left': (0, -1),
'right': (0, 1),
'up-left': (-1, -1),
'up-right': (-1, 1),
'down-left': (1, -1),
'down-right': (1, 1)
}
count = 0
#########################################
# #
# Part 1 #
# #
#########################################
if part == 1:
directions = {
'up': (-1, 0),
'down': (1, 0),
'left': (0, -1),
'right': (0, 1),
'up-left': (-1, -1),
'up-right': (-1, 1),
'down-left': (1, -1),
'down-right': (1, 1)
}
for r, rows in enumerate(grid):
for c, _ in enumerate(rows):
if get_value_in_direction(grid,(r,c)) == 'X':
for d in directions:
if get_value_in_direction(grid,(r,c),d,4,'str') == 'XMAS':
count += 1
print(count)
#########################################
# #
# Part 2 #
# #
#########################################
if part == 2:
# Kinda ugly code, wish it would be nicer.
for r, rows in enumerate(grid):
for c, _ in enumerate(rows):
tmp = ''
if get_value_in_direction(grid,(r,c)) == 'A':
if (get_value_in_direction(grid,(r,c),'up-left') == 'M' and get_value_in_direction(grid,(r,c),'down-right') == 'S') or (get_value_in_direction(grid,(r,c),'up-left') == 'S' and get_value_in_direction(grid,(r,c),'down-right') == 'M'):
if (get_value_in_direction(grid,(r,c),'up-right') == 'M' and get_value_in_direction(grid,(r,c),'down-left') == 'S') or (get_value_in_direction(grid,(r,c),'up-right') == 'S' and get_value_in_direction(grid,(r,c),'down-left') == 'M'):
count += 1
print(count)