From 096ceb0738591cd0aa3c5a59a3c17df23d66ac5c Mon Sep 17 00:00:00 2001 From: Daniel Weber <daniel.weber@gentex.com> Date: Mon, 16 Dec 2024 10:32:43 -0500 Subject: [PATCH] Part2 --- 2024/day16/main.py | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/2024/day16/main.py b/2024/day16/main.py index c759013..b7f57dd 100644 --- a/2024/day16/main.py +++ b/2024/day16/main.py @@ -31,8 +31,37 @@ def part_one(p, m): -def part_two(p, old_map): - pass +def part_two(p, m): + SCORES = {} + SCORES[tuple([0, *p])]=set([tuple(p[:2])]) + VISITED = {tuple(p[:2]): 0} + while True: + keys = list(SCORES.keys()) + keys.sort() + trail = SCORES.pop(keys[0]) + min = keys[0] + if m[min[1]][min[2]] == "E": + # print(trail) + # for x in trail: + # m[x[0]][x[1]] = "0" + # for x in m: + # print(''.join(x)) + return len(trail) + else: + s = tuple([min[0]+1, min[1]+min[3], min[2]+min[4], min[3], min[4]]) + sl = tuple([min[0]+1001, min[1]-min[4], min[2]+min[3], -min[4], min[3]]) + sr = tuple([min[0]+1001, min[1]+min[4], min[2]-min[3], min[4], -min[3]]) + for i in [s,sl,sr]: + if m[i[1]][i[2]] != "#": + if i[1:] not in VISITED or i[0] <= VISITED[i[1:]]: + new_trail = trail.copy() + new_trail.add(tuple(i[1:3])) + if i in SCORES: + SCORES[i] = SCORES[i].union(new_trail) + else: + SCORES[i] = new_trail + + VISITED[i[1:]] = i[0] def main(): @@ -45,7 +74,7 @@ def main(): pos[1]=cj print("P1: ", part_one(pos.copy(), copy.deepcopy(contents))) - part_two(pos.copy(), copy.deepcopy(contents)) + print("P2: ", part_two(pos.copy(), copy.deepcopy(contents))) if __name__ == "__main__": main()