Compare commits
3 commits
841830555c
...
a9c325ce14
Author | SHA1 | Date | |
---|---|---|---|
Bruno BELANYI | a9c325ce14 | ||
Bruno BELANYI | e457aaed44 | ||
Bruno BELANYI | 2c486e5984 |
|
@ -4,7 +4,7 @@ import dataclasses
|
|||
import itertools
|
||||
import sys
|
||||
from collections.abc import Iterator
|
||||
from typing import NamedTuple
|
||||
from typing import NamedTuple, Optional
|
||||
|
||||
|
||||
def sign(n: int) -> int:
|
||||
|
@ -57,7 +57,7 @@ def solve(input: list[str]) -> int:
|
|||
for dx, dy in ((0, 1), (-1, 1), (1, 1)):
|
||||
yield Point(p.x + dx, p.y + dy)
|
||||
|
||||
def add_sand(points: set[Point]) -> tuple[bool, set[Point]]:
|
||||
def add_sand(points: set[Point]) -> Optional[Point]:
|
||||
start = Point(500, 0)
|
||||
|
||||
assert start not in points # Sanity check
|
||||
|
@ -65,7 +65,7 @@ def solve(input: list[str]) -> int:
|
|||
while True:
|
||||
# Steady state was reached
|
||||
if start.y >= max_height:
|
||||
return False, points
|
||||
return None
|
||||
|
||||
viable_candidates = (p for p in sand_candidates(start) if p not in points)
|
||||
candidate = next(viable_candidates, None)
|
||||
|
@ -74,13 +74,14 @@ def solve(input: list[str]) -> int:
|
|||
break
|
||||
start = candidate
|
||||
|
||||
return True, (points | {start})
|
||||
return start
|
||||
|
||||
res = 0
|
||||
while True:
|
||||
added, all_points = add_sand(all_points)
|
||||
if not added:
|
||||
grain = add_sand(all_points)
|
||||
if grain is None:
|
||||
break
|
||||
all_points.add(grain)
|
||||
res += 1
|
||||
return res
|
||||
|
||||
|
|
|
@ -4,7 +4,7 @@ import dataclasses
|
|||
import itertools
|
||||
import sys
|
||||
from collections.abc import Iterator
|
||||
from typing import NamedTuple
|
||||
from typing import NamedTuple, Optional
|
||||
|
||||
|
||||
def sign(n: int) -> int:
|
||||
|
@ -57,11 +57,11 @@ def solve(input: list[str]) -> int:
|
|||
for dx, dy in ((0, 1), (-1, 1), (1, 1)):
|
||||
yield Point(p.x + dx, p.y + dy)
|
||||
|
||||
def add_sand(points: set[Point]) -> tuple[bool, set[Point]]:
|
||||
def add_sand(points: set[Point]) -> Optional[Point]:
|
||||
start = Point(500, 0)
|
||||
|
||||
if start in points:
|
||||
return False, points
|
||||
return None
|
||||
|
||||
while True:
|
||||
viable_candidates = (p for p in sand_candidates(start) if p not in points)
|
||||
|
@ -74,13 +74,14 @@ def solve(input: list[str]) -> int:
|
|||
break
|
||||
start = candidate
|
||||
|
||||
return True, (points | {start})
|
||||
return start
|
||||
|
||||
res = 0
|
||||
while True:
|
||||
added, all_points = add_sand(all_points)
|
||||
if not added:
|
||||
grain = add_sand(all_points)
|
||||
if grain is None:
|
||||
break
|
||||
all_points.add(grain)
|
||||
res += 1
|
||||
return res
|
||||
|
||||
|
|
Loading…
Reference in a new issue