From da8d8f6008c6e9b60048ba632b908a371826d57d Mon Sep 17 00:00:00 2001 From: Daniel Weber Date: Wed, 18 Dec 2024 08:47:54 -0500 Subject: [PATCH] Part 2 :) --- 2024/day18/main.py | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/2024/day18/main.py b/2024/day18/main.py index 46d0bd1..58dc7eb 100644 --- a/2024/day18/main.py +++ b/2024/day18/main.py @@ -1,3 +1,4 @@ +import re import sys import math import os @@ -22,7 +23,7 @@ def part_one(input): V = set() V.add(tuple(p)) G[tuple([mag(sub(END, p)), *p])] = [p] - while True: + while len(G)>0: keys = list(G.keys()) keys.sort() p_list = G.pop(keys[0]) @@ -30,21 +31,32 @@ def part_one(input): for x in [[1,0],[-1,0],[0,1],[0,-1]]: nx = add(x,p) if nx == END: - print("P1: ", len(p_list)) - return + return len(p_list) if grid_check(nx, input) and tuple(nx) not in V: h = tuple([mag(sub(END, nx)), *nx]) V.add(tuple(nx)) G[h] = [*p_list, nx] + return -1 def part_two(input): - pass + result = 0 + upper = len(input) + lower = INPUTS[2] + while upper != lower: + test = int(round((upper - lower+0.001)/2))+lower + result = part_one(input[:test]) + if result == -1: + upper = test-1 + else: + lower = test + print("P2: ", str(input[lower]).strip('[]').replace(" ", "")) + def main(): f = open(INPUTS[0], 'r') contents = [list(map(int, x.strip().split(","))) for x in f.readlines()] - part_one(contents[:INPUTS[2]]) - part_two(contents[:INPUTS[2]]) + print("P1: ", part_one(contents[:INPUTS[2]])) + part_two(contents) if __name__ == "__main__": main()