-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday15_solution.py
76 lines (55 loc) · 1.83 KB
/
day15_solution.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
day = 15
input_fn = f"day{day:02d}_input.txt"
with open(input_fn) as f:
data = f.read().split(",")
def hash_algorithm(string):
current_value = 0
for char in string:
current_value += ord(char)
current_value *= 17
current_value %= 256
return current_value
##################################################
# Part 1
##################################################
current_values = []
for string in data:
current_values.append(hash_algorithm(string))
# 511343
print(f"Part 1 answer: {sum(current_values)}")
##################################################
# Part 2
##################################################
# For part 2, string.find() to find the position of the symbol, then split the string up into parts.
# The number is always a single digit, so don’t need to worry about figuring out length of it.
labels = []
operations = []
focal_lengths = []
which_boxes = []
boxes = {num: {} for num in range(256)}
for item in data:
operation_position = item.find("=")
if operation_position == -1:
operation = "-"
operation_position = item.find("-")
focal_length = 0
else:
operation = "="
focal_length = int(item[operation_position+1:])
focal_lengths.append(focal_length)
label = item[:operation_position]
labels.append(label)
which_box = hash_algorithm(label)
operations.append(operation)
which_boxes.append(which_box)
if operation == "-":
boxes[which_box].pop(label, None)
else:
boxes[which_box][label] = focal_length
focusing_powers = []
for box, items in boxes.items():
for slot, (label, focal_length) in enumerate(items.items()):
focusing_power = (box + 1) * (slot + 1) * focal_length
focusing_powers.append(focusing_power)
# 294474
print(f"Part 2 answer: {sum(focusing_powers)}")