-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
81 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from utils import Point, lines | ||
from collections import deque, namedtuple | ||
|
||
def parse(lines): | ||
return [Point(*map(int, line.split(','))) for line in lines] | ||
|
||
bounds = Point(71, 71) | ||
|
||
Node = namedtuple('Node', ['pos', 'cost']) | ||
|
||
def solve(points): | ||
seen = set([(0,0)]) | ||
been = set() | ||
fringe = deque([Node(Point(0,0), 0)]) | ||
while fringe: | ||
cur = fringe.popleft() | ||
been.add(cur.pos) | ||
if cur.pos == bounds - (1, 1): | ||
return cur.cost | ||
for n in cur.pos.neighbors(): | ||
if n not in bounds or n in points: | ||
continue | ||
if n not in seen: | ||
seen.add(n) | ||
fringe.append(Node(n, cur.cost + 1)) | ||
return None | ||
# def draw(x, y): | ||
# if (x, y) in points: | ||
# return '#' | ||
# if (x, y) in been: | ||
# return 'O' | ||
# if (x, y) in seen: | ||
# return '.' | ||
# return ' ' | ||
# for y in range(bounds.y): | ||
# print(''.join(draw(x, y) for x in range(bounds.x))) | ||
|
||
if __name__ == '__main__': | ||
points = parse(lines()) | ||
print(solve(set(points[:1024]))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from utils import Point, lines | ||
from collections import deque, namedtuple | ||
from first import parse, solve | ||
|
||
if __name__ == '__main__': | ||
points = parse(lines()) | ||
fallen = set(points[:1024]) | ||
for p in points[1024:]: | ||
fallen.add(p) | ||
if not solve(fallen): | ||
print(f'{p.x},{p.y}') | ||
break |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from enum import IntEnum, auto | ||
|
||
def lines(): | ||
while True: | ||
try: | ||
yield input() | ||
except: | ||
break | ||
|
||
class Point: | ||
__match_args__ = ['x', 'y'] | ||
|
||
def __init__(self, x, y): self.x, self.y = x, y | ||
def neighbors(self): | ||
for d in ((1, 0), (0, 1), (-1, 0), (0, -1)): | ||
yield self + d | ||
def __add__(self, other): return Point(self.x + other[0], self.y + other[1]) | ||
def __mul__(self, sca): return Point(self.x * sca, self.y * sca) | ||
def __sub__(self, other): return Point(self.x - other[0], self.y - other[1]) | ||
def __neg__(self): return Point(-self.x, -self.y) | ||
def __eq__(self, other): return self.x == other[0] and self.y == other[1] | ||
def __getitem__(self, i): return self.x if i == 0 else self.y | ||
def __str__(self): return "({}, {})".format(self.x, self.y) | ||
def __repr__(self): return "({} {})".format(self.x, self.y) | ||
def __hash__(self): return (self.x, self.y).__hash__() | ||
def __contains__(self, other): return other[0] in range(self.x) and other[1] in range(self.y) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters