-
Notifications
You must be signed in to change notification settings - Fork 0
/
day15.py
60 lines (48 loc) · 1.62 KB
/
day15.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env python3
from collections import defaultdict
import numpy as np
import re
f = open("ressources/day15.txt", "r") #Open File
lines = f.readlines() #Separate in lines
def hash(numbers):
code = 0
for number in numbers:
code += number
code *= 17
code %= 256
return code
words = lines[0].split(',')
sum =0
for word in words:
asciiCode = [ord(char) for char in word]
hashCode = hash(asciiCode)
sum += hashCode
print("Sum of Hash Codes is: {}".format(int(sum)))
boxesLabels = defaultdict(list)
boxesFocals = defaultdict(list)
instructions = lines[0].split(',')
for instruction in instructions:
if '=' in instruction:
focal = int(instruction[-1])
label = instruction[:-2]
asciiCode = [ord(char) for char in label]
hashCode = hash(asciiCode)
if label in boxesLabels[hashCode]:
idx = boxesLabels[hashCode].index(label)
boxesFocals[hashCode][idx] = focal
else:
boxesLabels[hashCode].append(label)
boxesFocals[hashCode].append(focal)
if '-' in instruction:
label = instruction[:-1]
asciiCode = [ord(char) for char in label]
hashCode = hash(asciiCode)
if label in boxesLabels[hashCode]:
idx = boxesLabels[hashCode].index(label)
boxesFocals[hashCode].pop(idx)
boxesLabels[hashCode].pop(idx)
sum = 0
for boxNumber, focals in zip(boxesFocals.keys(), boxesFocals.values()):
for i, focal in enumerate(focals):
sum += (boxNumber+1)*(i+1)*focal
print("Sum of Focal Codes is: {}".format(int(sum)))