diff --git a/2016-python2/output/__init__.py b/2016-python2/output/__init__.py index 340e6cd..d68c1e0 100644 --- a/2016-python2/output/__init__.py +++ b/2016-python2/output/__init__.py @@ -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)] diff --git a/2016-python2/output/day_17.py b/2016-python2/output/day_17.py new file mode 100644 index 0000000..661139a --- /dev/null +++ b/2016-python2/output/day_17.py @@ -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)