[Python/2016] Move solutions into .py files

This commit is contained in:
Felix Bargfeldt 2023-10-29 12:38:12 +01:00
parent 0269ad8fc3
commit 2514b1d11f
Signed by: Defelo
GPG key ID: 2A05272471204DD3
50 changed files with 1172 additions and 3386 deletions

68
Python/2016/17.py Normal file
View file

@ -0,0 +1,68 @@
from lib import *
input = read_input(2016, 17).strip()
def get_state(path):
digest = hashlib.md5((input + path).encode()).hexdigest()
return [c >= "b" for c in digest[:4]] # up, down, left, right
def find_path():
queue = [("", 0, 0)]
while True:
path, x, y = queue.pop(0)
if x == y == 3:
return path
up, down, left, right = get_state(path)
if up and y > 0:
queue.append((path + "U", x, y - 1))
if down and y < 3:
queue.append((path + "D", x, y + 1))
if left and x > 0:
queue.append((path + "L", x - 1, y))
if right and x < 3:
queue.append((path + "R", x + 1, y))
print(find_path())
def find_path():
queue = [("", 0, 0)]
longest = ""
while queue:
path, x, y = queue.pop(0)
if x == y == 3:
if len(path) > len(longest):
longest = path
continue
up, down, left, right = get_state(path)
if up and y > 0:
queue.append((path + "U", x, y - 1))
if down and y < 3:
queue.append((path + "D", x, y + 1))
if left and x > 0:
queue.append((path + "L", x - 1, y))
if right and x < 3:
queue.append((path + "R", x + 1, y))
return longest
print(len(find_path()))