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 itertools
|
||||||
import sys
|
import sys
|
||||||
from collections.abc import Iterator
|
from collections.abc import Iterator
|
||||||
from typing import NamedTuple
|
from typing import NamedTuple, Optional
|
||||||
|
|
||||||
|
|
||||||
def sign(n: int) -> int:
|
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)):
|
for dx, dy in ((0, 1), (-1, 1), (1, 1)):
|
||||||
yield Point(p.x + dx, p.y + dy)
|
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)
|
start = Point(500, 0)
|
||||||
|
|
||||||
assert start not in points # Sanity check
|
assert start not in points # Sanity check
|
||||||
|
@ -65,7 +65,7 @@ def solve(input: list[str]) -> int:
|
||||||
while True:
|
while True:
|
||||||
# Steady state was reached
|
# Steady state was reached
|
||||||
if start.y >= max_height:
|
if start.y >= max_height:
|
||||||
return False, points
|
return None
|
||||||
|
|
||||||
viable_candidates = (p for p in sand_candidates(start) if p not in points)
|
viable_candidates = (p for p in sand_candidates(start) if p not in points)
|
||||||
candidate = next(viable_candidates, None)
|
candidate = next(viable_candidates, None)
|
||||||
|
@ -74,13 +74,14 @@ def solve(input: list[str]) -> int:
|
||||||
break
|
break
|
||||||
start = candidate
|
start = candidate
|
||||||
|
|
||||||
return True, (points | {start})
|
return start
|
||||||
|
|
||||||
res = 0
|
res = 0
|
||||||
while True:
|
while True:
|
||||||
added, all_points = add_sand(all_points)
|
grain = add_sand(all_points)
|
||||||
if not added:
|
if grain is None:
|
||||||
break
|
break
|
||||||
|
all_points.add(grain)
|
||||||
res += 1
|
res += 1
|
||||||
return res
|
return res
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,7 @@ import dataclasses
|
||||||
import itertools
|
import itertools
|
||||||
import sys
|
import sys
|
||||||
from collections.abc import Iterator
|
from collections.abc import Iterator
|
||||||
from typing import NamedTuple
|
from typing import NamedTuple, Optional
|
||||||
|
|
||||||
|
|
||||||
def sign(n: int) -> int:
|
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)):
|
for dx, dy in ((0, 1), (-1, 1), (1, 1)):
|
||||||
yield Point(p.x + dx, p.y + dy)
|
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)
|
start = Point(500, 0)
|
||||||
|
|
||||||
if start in points:
|
if start in points:
|
||||||
return False, points
|
return None
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
viable_candidates = (p for p in sand_candidates(start) if p not in points)
|
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
|
break
|
||||||
start = candidate
|
start = candidate
|
||||||
|
|
||||||
return True, (points | {start})
|
return start
|
||||||
|
|
||||||
res = 0
|
res = 0
|
||||||
while True:
|
while True:
|
||||||
added, all_points = add_sand(all_points)
|
grain = add_sand(all_points)
|
||||||
if not added:
|
if grain is None:
|
||||||
break
|
break
|
||||||
|
all_points.add(grain)
|
||||||
res += 1
|
res += 1
|
||||||
return res
|
return res
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue