Part 2 :)

This commit is contained in:
Daniel Weber 2024-12-18 08:47:54 -05:00
parent d050a1a6b3
commit da8d8f6008

View File

@ -1,3 +1,4 @@
import re
import sys import sys
import math import math
import os import os
@ -22,7 +23,7 @@ def part_one(input):
V = set() V = set()
V.add(tuple(p)) V.add(tuple(p))
G[tuple([mag(sub(END, p)), *p])] = [p] G[tuple([mag(sub(END, p)), *p])] = [p]
while True: while len(G)>0:
keys = list(G.keys()) keys = list(G.keys())
keys.sort() keys.sort()
p_list = G.pop(keys[0]) 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]]: for x in [[1,0],[-1,0],[0,1],[0,-1]]:
nx = add(x,p) nx = add(x,p)
if nx == END: if nx == END:
print("P1: ", len(p_list)) return len(p_list)
return
if grid_check(nx, input) and tuple(nx) not in V: if grid_check(nx, input) and tuple(nx) not in V:
h = tuple([mag(sub(END, nx)), *nx]) h = tuple([mag(sub(END, nx)), *nx])
V.add(tuple(nx)) V.add(tuple(nx))
G[h] = [*p_list, nx] G[h] = [*p_list, nx]
return -1
def part_two(input): 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(): def main():
f = open(INPUTS[0], 'r') f = open(INPUTS[0], 'r')
contents = [list(map(int, x.strip().split(","))) for x in f.readlines()] contents = [list(map(int, x.strip().split(","))) for x in f.readlines()]
part_one(contents[:INPUTS[2]]) print("P1: ", part_one(contents[:INPUTS[2]]))
part_two(contents[:INPUTS[2]]) part_two(contents)
if __name__ == "__main__": if __name__ == "__main__":
main() main()