day 4
This commit is contained in:
parent
f1b3953873
commit
cb5b2b97e0
|
|
@ -0,0 +1,52 @@
|
||||||
|
import aocd
|
||||||
|
|
||||||
|
from enum import Enum
|
||||||
|
|
||||||
|
class Direction(complex, Enum):
|
||||||
|
NORTH = -1j
|
||||||
|
SOUTH = 1j
|
||||||
|
EAST = -1
|
||||||
|
WEST = 1
|
||||||
|
NORTHWEST = NORTH+WEST
|
||||||
|
NORTHEAST = NORTH+EAST
|
||||||
|
SOUTHWEST = SOUTH+WEST
|
||||||
|
SOUTHEAST = SOUTH+EAST
|
||||||
|
|
||||||
|
class Paper:
|
||||||
|
def __init__(self, x, y, floor):
|
||||||
|
self.coords = complex(real=x, imag=y)
|
||||||
|
self.floor = floor
|
||||||
|
|
||||||
|
@property
|
||||||
|
def x(self):
|
||||||
|
return int(self.coords.real)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def y(self):
|
||||||
|
return int(self.coords.imag)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def neighbours(self):
|
||||||
|
yield from filter(None, (self.floor.get(self.coords + dir) for dir in Direction))
|
||||||
|
|
||||||
|
def parse(data):
|
||||||
|
floor = {}
|
||||||
|
|
||||||
|
for y, line in enumerate(data.split('\n')):
|
||||||
|
for x, char in enumerate(line):
|
||||||
|
if char == '@':
|
||||||
|
floor[complex(real=x, imag=y)] = Paper(x, y, floor)
|
||||||
|
|
||||||
|
return floor
|
||||||
|
|
||||||
|
def is_accessible(paper):
|
||||||
|
return sum(1 for _ in paper.neighbours) < 4
|
||||||
|
|
||||||
|
def main(data):
|
||||||
|
floor = parse(data.strip())
|
||||||
|
return sum(is_accessible(paper) for paper in floor.values())
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
solution = main(aocd.get_data(day=4, year=2025))
|
||||||
|
print(solution)
|
||||||
|
aocd.submit(solution, part='a', day=4, year=2025)
|
||||||
|
|
@ -0,0 +1,57 @@
|
||||||
|
import aocd
|
||||||
|
|
||||||
|
from enum import Enum
|
||||||
|
|
||||||
|
class Direction(complex, Enum):
|
||||||
|
NORTH = -1j
|
||||||
|
SOUTH = 1j
|
||||||
|
EAST = -1
|
||||||
|
WEST = 1
|
||||||
|
NORTHWEST = NORTH+WEST
|
||||||
|
NORTHEAST = NORTH+EAST
|
||||||
|
SOUTHWEST = SOUTH+WEST
|
||||||
|
SOUTHEAST = SOUTH+EAST
|
||||||
|
|
||||||
|
class Paper:
|
||||||
|
def __init__(self, x, y, floor):
|
||||||
|
self.coords = complex(real=x, imag=y)
|
||||||
|
self.floor = floor
|
||||||
|
|
||||||
|
@property
|
||||||
|
def x(self):
|
||||||
|
return int(self.coords.real)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def y(self):
|
||||||
|
return int(self.coords.imag)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def neighbours(self):
|
||||||
|
yield from filter(None, (self.floor.get(self.coords + dir) for dir in Direction))
|
||||||
|
|
||||||
|
def parse(data):
|
||||||
|
floor = {}
|
||||||
|
|
||||||
|
for y, line in enumerate(data.split('\n')):
|
||||||
|
for x, char in enumerate(line):
|
||||||
|
if char == '@':
|
||||||
|
floor[complex(real=x, imag=y)] = Paper(x, y, floor)
|
||||||
|
|
||||||
|
return floor
|
||||||
|
|
||||||
|
def is_accessible(paper):
|
||||||
|
return sum(1 for _ in paper.neighbours) < 4
|
||||||
|
|
||||||
|
def main(data):
|
||||||
|
floor = parse(data.strip())
|
||||||
|
total = 0
|
||||||
|
while (removable := set(paper for paper in floor.values() if is_accessible(paper))):
|
||||||
|
total += len(removable)
|
||||||
|
for paper in removable:
|
||||||
|
del floor[paper.coords]
|
||||||
|
return total
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
solution = main(aocd.get_data(day=4, year=2025))
|
||||||
|
print(solution)
|
||||||
|
aocd.submit(solution, part='b', day=4, year=2025)
|
||||||
Loading…
Reference in New Issue