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)