Finished 2023-12-15 p1 + p2
This commit is contained in:
parent
21944b2c8f
commit
141dfa3282
64
2023/day15/part2.py
Normal file
64
2023/day15/part2.py
Normal file
@ -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)
|
Loading…
Reference in New Issue
Block a user