-
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
3 changed files
with
80 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,55 @@ | ||
from utils import lines, Point | ||
from collections import deque, namedtuple, defaultdict | ||
|
||
Node = namedtuple('Node', ['position', 'origin']) | ||
|
||
def parse(lines): | ||
heights = {} | ||
trailheads = [] | ||
peaks = [] | ||
for y, line in enumerate(lines): | ||
for x, h in enumerate(map(int, line)): | ||
p = Point(x, y) | ||
heights[p] = h | ||
if h == 0: | ||
trailheads.append(p) | ||
if h == 9: | ||
peaks.append(p) | ||
return heights, trailheads, peaks | ||
|
||
def part1(heights, peaks): | ||
reachable = defaultdict(set) | ||
score = defaultdict(int) | ||
fringe = deque(Node(p, p) for p in peaks) | ||
while fringe: | ||
pos, origin = fringe.popleft() | ||
for n in pos.neighbors(): | ||
if n not in heights or heights[n] != heights[pos] - 1 or n in reachable[origin]: | ||
continue | ||
reachable[origin].add(n) | ||
if heights[n] == 0: | ||
score[n] += 1 | ||
else: | ||
fringe.append(Node(n, origin)) | ||
return sum(score.values()) | ||
|
||
def part2(heights, trailheads): | ||
rating = defaultdict(int) | ||
fringe = deque(Node(p, p) for p in trailheads) | ||
while fringe: | ||
pos, origin = fringe.popleft() | ||
for n in pos.neighbors(): | ||
if n not in heights or heights[n] != heights[pos] + 1: | ||
continue | ||
if heights[n] == 9: | ||
rating[origin] += 1 | ||
else: | ||
fringe.append(Node(n, origin)) | ||
return sum(rating.values()) | ||
|
||
|
||
|
||
if __name__ == '__main__': | ||
heights, trailheads, peaks = parse(lines()) | ||
print(part1(heights, peaks)) | ||
print(part2(heights, trailheads)) |
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,22 @@ | ||
def lines(): | ||
while True: | ||
try: | ||
yield input() | ||
except: | ||
break | ||
|
||
class Point: | ||
def __init__(self, x, y): self.x, self.y = x, y | ||
def neighbor(self, D): return self + [(0,-1),(1,0),(0,1),(-1,0)][D] | ||
def neighbors(self): | ||
for i in range(4): | ||
yield self.neighbor(i) | ||
def __add__(self, other): return Point(self.x + other[0], self.y + other[1]) | ||
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.x in range(self.x) and other.y 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