day14 - p2 faster
This commit is contained in:
parent
4c3332a731
commit
d5a59a80f5
29
day14.py
29
day14.py
@ -1,3 +1,4 @@
|
||||
from collections import deque
|
||||
from enum import Enum
|
||||
from tools.aoc import AOCDay
|
||||
from tools.coordinate import Coordinate
|
||||
@ -65,23 +66,19 @@ class Day(AOCDay):
|
||||
|
||||
def part2(self) -> Any:
|
||||
cave = self.get_cave()
|
||||
y = cave.maxY + 2
|
||||
for x in range(cave.minX - 200, cave.maxX + 200):
|
||||
cave.set(Coordinate(x, y), TType.ROCK)
|
||||
v = set()
|
||||
q = deque()
|
||||
q.append(SAND_SOURCE)
|
||||
while q:
|
||||
current = q.pop()
|
||||
if current in v or current.y > cave.maxY + 1:
|
||||
continue
|
||||
v.add(current)
|
||||
for c in [Coordinate(current.x, current.y + 1), Coordinate(current.x - 1, current.y + 1), Coordinate(current.x + 1, current.y + 1)]:
|
||||
if cave.get(c) == TType.AIR:
|
||||
q.append(c)
|
||||
|
||||
while cave.get(SAND_SOURCE) == TType.AIR:
|
||||
tx, ty = 0, 0
|
||||
while ty <= cave.maxY:
|
||||
ty += 1
|
||||
for c in [Coordinate(tx, ty), Coordinate(tx - 1, ty), Coordinate(tx + 1, ty)]:
|
||||
if cave.get(SAND_SOURCE + c) == TType.AIR:
|
||||
tx += compare(c.x, tx)
|
||||
break
|
||||
else:
|
||||
cave.set(SAND_SOURCE + Coordinate(tx, ty - 1), TType.SAND)
|
||||
break
|
||||
|
||||
return cave.count(TType.SAND)
|
||||
return len(v)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
Loading…
Reference in New Issue
Block a user