From 09d3f25b092e86df8be55114ddecb547a9c75c13 Mon Sep 17 00:00:00 2001 From: Julia Lange Date: Mon, 12 Jan 2026 11:43:49 -0800 Subject: [PATCH] 2025 04 --- Python/2025/04/main.py | 47 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 Python/2025/04/main.py diff --git a/Python/2025/04/main.py b/Python/2025/04/main.py new file mode 100644 index 0000000..78366d1 --- /dev/null +++ b/Python/2025/04/main.py @@ -0,0 +1,47 @@ +input = open("input", 'r') + +cardinal_offset = [x+y for x in [-1,0,1] for y in [-1j,0,1j] if x+y != 0] + +papermap = set() +for x, line in enumerate(input): + for y, item in enumerate(line.rstrip()): + if item == "@": + papermap.add(complex(x,y)) + +def part1(pmap, adjacent_min=4): + accessible = set() + for location in pmap: + adjacents = 0 + for offset in cardinal_offset: + adjacents += 1 if location+offset in pmap else 0 + if adjacents < adjacent_min: + accessible.add(location) + print(len(accessible)) + +def part2(pmap, adjacent_min=4): + amap = {} + to_remove = set() + # Build adjacency map + for location in pmap: + adjacents = 0 + for offset in cardinal_offset: + adjacents += 1 if location+offset in pmap else 0 + amap[location] = adjacents + if adjacents < adjacent_min: + to_remove.add(location) + + starting_value = len(amap) + + while to_remove: + rm_location = to_remove.pop() + del amap[rm_location] + for offset in cardinal_offset: + if rm_location+offset in amap: + amap[rm_location+offset] -= 1 + if amap[rm_location+offset] < adjacent_min: + to_remove.add(rm_location+offset) + + print(starting_value-len(amap)) + +part1(papermap) +part2(papermap)