Files
advent-of-code/2025/day04/part2.py

116 lines
3.8 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Читаем инпут и создаем масив состоящий из списков в списке
# Х это первы список и "вертикалб"
# У это подсписок с "горизонталь"
input = """..@@.@@@@.
@@@.@.@.@@
@@@@@.@.@@
@.@@@@..@.
@@.@@@@.@@
.@@@@@@@.@
.@.@.@.@@@
@.@@@.@@@@
.@@@@@@@@.
@.@.@@@.@."""
with open("input.txt", "r") as file:
input = file.read()
karta = []
for row in input.splitlines():
karta.append(list(map(str, row.strip())))
for row in karta:
print("".join(row))
# проверка существования поля
def fild_exist(x: int, y: int):
if x < 0 or x > len(karta) - 1:
return False
elif y < 0 or y > len(karta[x]) - 1:
return False
else:
return True
# Функция проверки
def cheker(x: int, y: int):
roll_count = 0
filds_to_check = 9
# print(f"cheker for {x} {y}")
# print(f"roll_count {roll_count}\nfilds_to_check {filds_to_check}")
while (int(roll_count) >= 4) is False or (filds_to_check <= 0) is False:
# print(f"Предположительные координаты для range: x {list(range(x - 1,x + 2))}, y {list(range(y - 1, y +2))}")
for n_x in list(range(x - 1, x + 2)):
for n_y in list(range(y - 1, y + 2)):
# print(f"Существует поле {n_x} {n_y}? {fild_exist(n_x,n_y)}")
if fild_exist(n_x, n_y):
if karta[n_x][n_y] != "." and not (n_x == x and n_y == y):
roll_count += 1
filds_to_check -= 1
# print(f"ADD +1 to roll_count\nroll_count {roll_count}\nfilds_to_check {filds_to_check}")
elif n_x == x and n_y == y:
# print(f"Do not self chek!!!\nroll_count {roll_count}\nfilds_to_check {filds_to_check}")
filds_to_check -= 1
else:
# print(f"field has a .\nroll_count {roll_count}\nfilds_to_check {filds_to_check}")
filds_to_check -= 1
else:
# print(f"FIeld dos'n exist\nroll_count {roll_count}\nfilds_to_check {filds_to_check}")
filds_to_check -= 1
# print("FINAL roll_count =",roll_count)
# print(filds_to_check <= 0)
# print("="*100)
return roll_count
# Основное тело
def reducer():
result = 0
print("*" * (len(karta[0])), " Начало reducer")
for row in karta:
print("".join(row))
for x, val_x in enumerate(karta):
for y, val_y in enumerate(karta[x]):
# print(f"проверяем координаты: x={x}, y={y}")
if karta[x][y] != ".":
if cheker(x, y) < 4:
karta[x][y] = "x"
result += 1
# print(f"cheker for {x} {y} is {cheker(x,y)}")
else:
pass
else:
pass
print("*" * (len(karta[0])), " Конец reducer")
for row in karta:
print("".join(row))
return result
old_result = 0
new_result = reducer()
all_result = new_result
print(f"new_result: {new_result}")
print("-" * (len(karta[0])), "НАЧАЛО While")
while new_result != 0:
old_result = new_result
for row in karta:
for i, r in enumerate(row):
row[i] = r.replace("x", ".")
for row in karta:
print("".join(row))
print("-" * (len(karta[0])), " После замены x на .")
new_result = reducer()
print(f"Remove {abs(new_result - old_result)} roll of paper\n")
print(f"old_result: {old_result} new_result: {new_result}")
all_result += new_result
print("=" * (len(karta[0])))
for row in karta:
print("".join(row))
print("Result", all_result)