AdventOfCode/2023/day15/part2.py

64 lines
1.2 KiB
Python

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)