-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
81 lines (65 loc) · 2.69 KB
/
main.py
File metadata and controls
81 lines (65 loc) · 2.69 KB
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
76
77
78
79
80
81
import time
hashmap = {}
def hash_label(label, prev_node=0):
current_value = prev_node
for char in label:
current_value += ord(char)
current_value *= 17
current_value %= 256
return current_value
def hash_sequence(sequence, stage=1):
sequence_hash = 0
for code in sequence:
operation = "-" if "-" in code else "="
label, focal_length = code.split(operation)
lbl_hash = hash_label(label)
if stage == 2:
global hashmap
hashmap[label] = lbl_hash
sequence_hash += hash_label(operation+focal_length, lbl_hash)
return sequence_hash
def main(input_file, stage=1):
with open(input_file) as file:
sequence = file.readlines()
sequence = sequence[0].strip().split(",")
sequence_hash = hash_sequence(sequence, stage)
if stage == 1:
print(sequence_hash)
return
# commented out is the first version using a list of tuples instead of a dict
# didn't know at first that dicts keep the order of insertion
#boxes = [[] for _ in range(256)]
boxes = [{} for _ in range(256)]
global hashmap
for code in sequence:
operation = "-" if "-" in code else "="
label, focal_length = code.split(operation)
#box_num = 0
#while box_num < len(boxes[hashmap[label]]):
# if boxes[hashmap[label]][box_num][0] == label:
# break
# box_num += 1
if operation == "-":
#if box_num < len(boxes[hashmap[label]]):
if boxes[hashmap[label]].get(label, None) is not None:
del boxes[hashmap[label]][label]
if operation == "=":
boxes[hashmap[label]][label] = int(focal_length)
#if box_num < len(boxes[hashmap[label]]):
# boxes[hashmap[label]][box_num][1] = int(focal_length)
#else:
# boxes[hashmap[label]].append([label, int(focal_length)])
focusing_power = 0
for i in range(len(boxes)):
focusing_power_lenses = [(i+1) * (j+1) * boxes[i][slot] for j, slot in enumerate(boxes[i].keys())]
focusing_power += sum(focusing_power_lenses)
print(focusing_power)
if __name__ == "__main__":
use_example = False
file_name = "example" if use_example else "input"
start_time = time.time()
main(file_name, 1)
print(f"Stage 1 time: {time.time()-start_time:.10f}")
start_time = time.time()
main(file_name, 2)
print(f"Stage 2 time: {time.time()-start_time:.10f}")