Compare commits
4 commits
4bad7e1de4
...
969778f724
Author | SHA1 | Date | |
---|---|---|---|
Bruno BELANYI | 969778f724 | ||
Bruno BELANYI | 914b343145 | ||
Bruno BELANYI | 30b93baff1 | ||
Bruno BELANYI | d7d8f12774 |
65
2024/d18/ex1/ex1.py
Executable file
65
2024/d18/ex1/ex1.py
Executable file
|
@ -0,0 +1,65 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
import heapq
|
||||||
|
import sys
|
||||||
|
from typing import Iterator, NamedTuple
|
||||||
|
|
||||||
|
|
||||||
|
class Point(NamedTuple):
|
||||||
|
x: int
|
||||||
|
y: int
|
||||||
|
|
||||||
|
def neighbours(self) -> Iterator["Point"]:
|
||||||
|
for dx, dy in (
|
||||||
|
(-1, 0),
|
||||||
|
(1, 0),
|
||||||
|
(0, -1),
|
||||||
|
(0, 1),
|
||||||
|
):
|
||||||
|
yield Point(self.x + dx, self.y + dy)
|
||||||
|
|
||||||
|
|
||||||
|
DIMS = Point(70, 70)
|
||||||
|
|
||||||
|
|
||||||
|
def solve(input: str) -> int:
|
||||||
|
def parse(input: list[str]) -> list[Point]:
|
||||||
|
return [Point(*map(int, line.split(","))) for line in input]
|
||||||
|
|
||||||
|
def djikstra(start: Point, end: Point, blocks: set[Point]) -> int:
|
||||||
|
# Priority queue of (distance, point)
|
||||||
|
queue = [(0, start)]
|
||||||
|
seen: set[Point] = set()
|
||||||
|
|
||||||
|
while len(queue) > 0:
|
||||||
|
cost, p = heapq.heappop(queue)
|
||||||
|
if p == end:
|
||||||
|
return cost
|
||||||
|
# We must have seen p with a smaller distance before
|
||||||
|
if p in seen:
|
||||||
|
continue
|
||||||
|
# First time encountering p, must be the smallest distance to it
|
||||||
|
seen.add(p)
|
||||||
|
# Add all neighbours to be visited
|
||||||
|
for n in p.neighbours():
|
||||||
|
if p in blocks:
|
||||||
|
continue
|
||||||
|
if not 0 <= p.x <= DIMS.x:
|
||||||
|
continue
|
||||||
|
if not 0 <= p.y <= DIMS.y:
|
||||||
|
continue
|
||||||
|
heapq.heappush(queue, (cost + 1, n))
|
||||||
|
|
||||||
|
assert False # Sanity check
|
||||||
|
|
||||||
|
coords = parse(input.splitlines())
|
||||||
|
return djikstra(Point(0, 0), DIMS, set(coords[:1024]))
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> None:
|
||||||
|
input = sys.stdin.read()
|
||||||
|
print(solve(input))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
3450
2024/d18/ex1/input
Normal file
3450
2024/d18/ex1/input
Normal file
File diff suppressed because it is too large
Load diff
77
2024/d18/ex2/ex2.py
Executable file
77
2024/d18/ex2/ex2.py
Executable file
|
@ -0,0 +1,77 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
import heapq
|
||||||
|
import sys
|
||||||
|
from typing import Iterator, NamedTuple
|
||||||
|
|
||||||
|
|
||||||
|
class Point(NamedTuple):
|
||||||
|
x: int
|
||||||
|
y: int
|
||||||
|
|
||||||
|
def neighbours(self) -> Iterator["Point"]:
|
||||||
|
for dx, dy in (
|
||||||
|
(-1, 0),
|
||||||
|
(1, 0),
|
||||||
|
(0, -1),
|
||||||
|
(0, 1),
|
||||||
|
):
|
||||||
|
yield Point(self.x + dx, self.y + dy)
|
||||||
|
|
||||||
|
|
||||||
|
DIMS = Point(70, 70)
|
||||||
|
|
||||||
|
|
||||||
|
def solve(input: str) -> str:
|
||||||
|
def parse(input: list[str]) -> list[Point]:
|
||||||
|
return [Point(*map(int, line.split(","))) for line in input]
|
||||||
|
|
||||||
|
def djikstra(start: Point, end: Point, blocks: set[Point]) -> int | None:
|
||||||
|
# Priority queue of (distance, point)
|
||||||
|
queue = [(0, start)]
|
||||||
|
seen: set[Point] = set()
|
||||||
|
|
||||||
|
while len(queue) > 0:
|
||||||
|
cost, p = heapq.heappop(queue)
|
||||||
|
if p == end:
|
||||||
|
return cost
|
||||||
|
# We must have seen p with a smaller distance before
|
||||||
|
if p in seen:
|
||||||
|
continue
|
||||||
|
# First time encountering p, must be the smallest distance to it
|
||||||
|
seen.add(p)
|
||||||
|
# Add all neighbours to be visited
|
||||||
|
for n in p.neighbours():
|
||||||
|
if p in blocks:
|
||||||
|
continue
|
||||||
|
if not 0 <= p.x <= DIMS.x:
|
||||||
|
continue
|
||||||
|
if not 0 <= p.y <= DIMS.y:
|
||||||
|
continue
|
||||||
|
heapq.heappush(queue, (cost + 1, n))
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
def bisect_cutoff(start: Point, end: Point, blocks: list[Point]) -> Point:
|
||||||
|
# Cutting off the path is monotonic: once cut-off, it's never uncut
|
||||||
|
low, high = 0, len(blocks)
|
||||||
|
while low < high:
|
||||||
|
mid = low + (high - low + 1) // 2
|
||||||
|
if djikstra(start, end, set(blocks[:mid])) is None:
|
||||||
|
high = mid - 1
|
||||||
|
else:
|
||||||
|
low = mid
|
||||||
|
return blocks[low]
|
||||||
|
|
||||||
|
coords = parse(input.splitlines())
|
||||||
|
byte = bisect_cutoff(Point(0, 0), DIMS, coords)
|
||||||
|
return f"{byte.x},{byte.y}"
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> None:
|
||||||
|
input = sys.stdin.read()
|
||||||
|
print(solve(input))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
3450
2024/d18/ex2/input
Normal file
3450
2024/d18/ex2/input
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue