-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
40 lines (34 loc) · 1.33 KB
/
main.py
File metadata and controls
40 lines (34 loc) · 1.33 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
import time
def main(input_file, stage):
with open(input_file, "r") as file:
rucksacks = [rucksack.strip() for rucksack in file.readlines()]
dupes = []
if stage == 1:
for rucksack in rucksacks:
front, back = rucksack[:len(rucksack)//2], rucksack[len(rucksack)//2:]
dupe = None
for item in front:
if item in back:
dupes.append(item)
break
else:
for i in range(0, len(rucksacks), 3):
b_elf1, b_elf2, b_elf3 = rucksacks[i], rucksacks[i+1], rucksacks[i+2]
for item in b_elf1:
if item in b_elf2 and item in b_elf3:
dupes.append(item)
break
item_priorities = 0
for dupe in dupes:
if dupe is not None:
item_priorities += ord(dupe) - (ord("`") if str.islower(dupe) else ord("&"))
print(item_priorities)
if __name__ == "__main__":
use_example = False
file_name = "example" if use_example else "input"
start = time.time()
main(file_name, 1)
print(f"Stage 1 time: {time.time() - start}")
start = time.time()
main(file_name, 2)
print(f"Stage 2 time: {time.time() - start}")