Solve 2016:17 p1-2 "Two Steps Forward"
Generators baby.
This commit is contained in:
parent
63a7ccd0e2
commit
3170846595
2 changed files with 41 additions and 22 deletions
|
|
@ -30,28 +30,6 @@ ADJ = [
|
|||
]
|
||||
|
||||
|
||||
def answer(part_index, fmt_string):
|
||||
"""Decorator to present a solution in a human readable format"""
|
||||
|
||||
def decorator_aoc(func):
|
||||
@functools.wraps(func)
|
||||
def wrapper_aoc(*args, **kwargs):
|
||||
decorate = kwargs.get("decorate", False)
|
||||
if decorate:
|
||||
del kwargs["decorate"]
|
||||
answer = func(*args, **kwargs)
|
||||
if not decorate:
|
||||
print(answer)
|
||||
else:
|
||||
formatted = fmt_string.format(answer)
|
||||
print(f" {part_index}) {formatted}")
|
||||
return answer
|
||||
|
||||
return wrapper_aoc
|
||||
|
||||
return decorator_aoc
|
||||
|
||||
|
||||
def ints(s):
|
||||
"""Extract all integers from a string"""
|
||||
return [int(n) for n in re.findall(r"\d+", s)]
|
||||
|
|
|
|||
41
2016-python2/output/day_17.py
Normal file
41
2016-python2/output/day_17.py
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
from collections import deque
|
||||
from hashlib import md5
|
||||
|
||||
from output import DD
|
||||
|
||||
|
||||
def solve(data):
|
||||
paths = list(bfs(data))
|
||||
p1 = "".join(paths[0])
|
||||
p2 = len(paths[-1])
|
||||
return p1, p2
|
||||
|
||||
|
||||
def bfs(code):
|
||||
q = deque([((0, 0), [])])
|
||||
T = (3, 3)
|
||||
K = "UDLR"
|
||||
while q:
|
||||
m, d = q.popleft()
|
||||
U, D, L, R, *_ = md5((code + "".join(d)).encode()).hexdigest()
|
||||
y, x = m
|
||||
for udlr in [K[k] for k, n in enumerate([U, D, L, R]) if isopen(n)]:
|
||||
dy, dx = DD[udlr]
|
||||
if (y + dy, x + dx) == T:
|
||||
yield d + [udlr]
|
||||
elif 0 <= y + dy < 4 and 0 <= x + dx < 4:
|
||||
q.append(((y + dy, x + dx), d + [udlr]))
|
||||
|
||||
|
||||
def isopen(c):
|
||||
return c in "bcdef"
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
with open("./input/17.txt", "r") as f:
|
||||
inp = f.read().strip()
|
||||
|
||||
p1, p2 = solve(inp)
|
||||
|
||||
print(p1)
|
||||
print(p2)
|
||||
Loading…
Add table
Reference in a new issue