2025 - Day 4 part 2
Some checks reported errors
continuous-integration/drone/push Build encountered an error

This commit is contained in:
kleph 2025-12-05 18:04:57 +01:00
parent fdef2f407b
commit bea0c5acef

57
2025/4/4_2.py Normal file
View file

@ -0,0 +1,57 @@
#!/usr/bin/env python
# 2025 - Advent Of Code 4 part 2
# file = 'input_example.txt'
file = 'input.txt'
# pylint: disable=consider-using-with
input_lines = [list(line.strip('\n')) for line in open(file, encoding="utf-8")]
MAX_X = len(input_lines[0])
MAX_Y = len(input_lines)
for pos_y in range(0, MAX_Y):
for pos_x in range(0, MAX_X):
print(input_lines[pos_y][pos_x], end='')
print()
print()
total_removed = 0
accessible = 1
while accessible != 0:
new_grid = [[input_lines[y][x] for x in range(MAX_X)] for y in range(MAX_Y)]
accessible = 0
for pos_y in range(0, MAX_Y):
for pos_x in range(0, MAX_X):
if input_lines[pos_y][pos_x] == '@':
rolls = 0
if pos_y > 0 and pos_x > 0 and input_lines[pos_y-1][pos_x-1] == '@':
rolls += 1
if pos_y > 0 and input_lines[pos_y-1][pos_x] == '@':
rolls += 1
if pos_y > 0 and pos_x < MAX_X - 1 and input_lines[pos_y-1][pos_x+1] == '@':
rolls += 1
if pos_x > 0 and input_lines[pos_y][pos_x-1] == '@':
rolls += 1
if pos_x < MAX_X - 1 and input_lines[pos_y][pos_x+1] == '@':
rolls += 1
if pos_y < MAX_Y - 1 and pos_x > 0 and input_lines[pos_y+1][pos_x-1] == '@':
rolls += 1
if pos_y < MAX_Y - 1 and input_lines[pos_y+1][pos_x] == '@':
rolls += 1
if pos_y < MAX_Y - 1 and pos_x < MAX_X - 1 and input_lines[pos_y+1][pos_x+1] == '@':
rolls += 1
if rolls < 4:
accessible += 1
new_grid[pos_y][pos_x] = 'x'
else:
new_grid[pos_y][pos_x] = input_lines[pos_y][pos_x]
# for pos_y in range(0, MAX_Y):
# for pos_x in range(0, MAX_X):
# print(input_lines[pos_y][pos_x], end='')
# print()
input_lines = new_grid.copy()
total_removed += accessible
print(total_removed)