From 141dfa32822f3afac71ed0e2b31601739295b796 Mon Sep 17 00:00:00 2001 From: Frederik Baerentsen Date: Fri, 15 Dec 2023 19:45:22 -0500 Subject: [PATCH] Finished 2023-12-15 p1 + p2 --- 2023/day15/part2.py | 64 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 2023/day15/part2.py diff --git a/2023/day15/part2.py b/2023/day15/part2.py new file mode 100644 index 0000000..8d82ed5 --- /dev/null +++ b/2023/day15/part2.py @@ -0,0 +1,64 @@ +import sys +import os +from pprint import pprint +import time +import math +from collections import defaultdict + +#colors +from termcolor import colored + +grid = [] + +input_f = '' +if len(sys.argv) == 1: + input_f = 'test' +else: + input_f = sys.argv[1] + +with open(input_f) as file: + for line in file: + grid = line.rstrip().split(',') + +print(grid) +values = [] +current_value = 0 + +boxes = defaultdict(dict) + +for i in grid: + value = 0 + name = '' + for jdx,j in enumerate(list(i)): + if j.isalpha(): + name += j + current_value += value + #print(j) + current_value += ord(j) + current_value *= 17 + current_value %= 256 + #print(current_value) + + if j == '-': + boxes[current_value].pop(name,None) + if j == '=': + boxes[current_value][name] = int(list(i)[jdx+1]) + + + value = current_value + current_value = 0 + #print(i,end=' ') + #print('\t',end='') + #print(name,end=' ') + #print('\t',end='') + #print(value) + #pprint(boxes) + #print() + +value = 0 + +for idx,i in enumerate(boxes): + for jdx,j in enumerate(boxes[i].values()): + #print(i+1,jdx+1,j,end=' ') + value += ((i+1)*(jdx+1)*j) +print(value) \ No newline at end of file