-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3_12.py
More file actions
55 lines (46 loc) · 2.01 KB
/
3_12.py
File metadata and controls
55 lines (46 loc) · 2.01 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
# get common item in rucksacks - set intersection
# get priority of that item - index+1 of letter in alphabet list (lower-case + upper-case)
# put in list, then sum
import string
from collections import Counter
def return_priority_from_toy(toy):
latin_alphabet=list(string.ascii_letters)
priority=latin_alphabet.index(common_toy)+1
return priority
f=open('input_3_12')
list_toys=[line.strip() for line in f.readlines()]
list_priorities=[]
list_priorities_with_mult=[]
list_priorities_pt2=[]
for i_line,toys in enumerate(list_toys):
# part 1
n_toys=len(toys)
toys_compartment_1=toys[:int(n_toys/2)]
toys_compartment_2=toys[int(n_toys/2):]
common_toys=set(toys_compartment_1).intersection(set(toys_compartment_2)) # which are the common toys
if len(common_toys)==0:
pass
else:
for common_toy in common_toys:
priority=return_priority_from_toy(common_toy)
list_priorities.append(priority)
# the part below is only if the same item on both sides had to be counted more than once for the priority count
counter_compartment_1=Counter(toys_compartment_1)
counter_compartment_2=Counter(toys_compartment_2)
mult=min(counter_compartment_1[common_toy],counter_compartment_2[common_toy])
list_priorities_with_mult.append(mult*priority)
# part 2
if i_line%3==0:
toys_elf_1=list_toys[i_line]
toys_elf_2=list_toys[i_line+1]
toys_elf_3=list_toys[i_line+2]
common_toys_3_rucksacks=set(toys_elf_1).intersection(set(toys_elf_2),set(toys_elf_3))
for common_toy in common_toys_3_rucksacks:
priority_pt2= return_priority_from_toy(common_toy)
list_priorities_pt2.append(priority_pt2)
sum_priorities=sum(list_priorities)
sum_priorities_with_mult=sum(list_priorities_with_mult)
sum_priorities_pt2=sum(list_priorities_pt2)
print(sum_priorities)
print(sum_priorities_with_mult)
print(sum_priorities_pt2)