2025 04
This commit is contained in:
parent
c700ff9bb6
commit
09d3f25b09
1 changed files with 47 additions and 0 deletions
47
Python/2025/04/main.py
Normal file
47
Python/2025/04/main.py
Normal file
|
|
@ -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)
|
||||||
Loading…
Add table
Add a link
Reference in a new issue