From 22dec5e521e5b7391850684d0d891391d12e6cb5 Mon Sep 17 00:00:00 2001 From: Kees van Kempen Date: Fri, 5 Dec 2025 09:42:33 +0100 Subject: [PATCH] 2025(04): solve part 2, change to function to what they should have been in 1 --- 2025/04/part1.py | 8 ++++++-- 2025/04/part2.py | 24 ++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 2025/04/part2.py diff --git a/2025/04/part1.py b/2025/04/part1.py index d2a6831..3adb6dc 100644 --- a/2025/04/part1.py +++ b/2025/04/part1.py @@ -36,14 +36,18 @@ def count_neighbours(tp_map): dX, dY = X.copy() + x, Y.copy() + y legal_idx = (dX >= 0)*(dX < L_x)*(dY >= 0)*(dY < L_y)*((dX != x)|(dY != y)) neighbour_counts[x, y] = (tp_map[dX[legal_idx], dY[legal_idx]]).sum() + return neighbour_counts + +def count_extractable(tp_map): + neighbour_counts = count_neighbours(tp_map) max_rolls = 4 return (neighbour_counts[tp_map] < max_rolls).sum() if __name__ == "__main__": test_map = read_map("testinput") # print(test_map) - assert count_neighbours(test_map) == 13 + assert count_extractable(test_map) == 13 # TODO: Compare the output map. real_map = read_map("input") - print(count_neighbours(real_map)) \ No newline at end of file + print(count_extractable(real_map)) \ No newline at end of file diff --git a/2025/04/part2.py b/2025/04/part2.py new file mode 100644 index 0000000..b276561 --- /dev/null +++ b/2025/04/part2.py @@ -0,0 +1,24 @@ +import numpy as np +from part1 import read_map, print_map, count_neighbours, count_extractable + +def remove_iteratively(tp_map): + max_rolls = 4 + rolls_removed_counts = [] + rolls_removed_count = 1_000_000 + while rolls_removed_count > 0: + neighbour_counts = count_neighbours(tp_map) + rolls_to_remove = (neighbour_counts < max_rolls)*tp_map + rolls_removed_count = rolls_to_remove.sum() + rolls_removed_counts.append(rolls_removed_count) + tp_map[rolls_to_remove] = False + return sum(rolls_removed_counts) + +if __name__ == "__main__": + test_map = read_map("testinput") + # print(test_map) + assert count_extractable(test_map) == 13 + assert remove_iteratively(test_map.copy()) == 43 + # TODO: Compare the output maps. + + real_map = read_map("input") + print(remove_iteratively(real_map.copy())) \ No newline at end of file