diff --git a/day14.py b/day14.py index eb9e373..91e9de3 100644 --- a/day14.py +++ b/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__':