-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday3_ruck.py
91 lines (65 loc) · 2.31 KB
/
day3_ruck.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import sys
from math import floor
def ParseInput(filename):
sacks = None
with open(filename) as f:
sacks = f.read().splitlines()
return sacks
def LetterToPriority(letter):
return ord(letter) - 64 + 26 if letter.isupper() else ord(letter) - 64 - 32
def CalculatePriority(sack):
half_sack = floor(len(sack)/2)
sack1 = sack[:half_sack]
sack2 = sack[half_sack:]
not_allowed = []
sack_priority = 0
for letter in sack1:
if letter in sack2 and letter not in not_allowed:
sack_priority += LetterToPriority(letter)
not_allowed.append(letter)
return sack_priority
def FindGroups(sacks):
not_allowed = []
potential = []
for letter in sacks[0]:
if letter in sacks[1] and letter not in not_allowed:
potential.append(letter)
else:
not_allowed.append(letter)
for letter in potential:
if letter in sacks[2] and letter not in not_allowed:
return letter
print(sacks)
potential = list(set(sacks[0]))
potential_copy = [letter for letter in potential]
for letter in potential:
if letter not in sacks[1]:
# print(letter)
potential_copy.remove(letter)
print(potential)
potential = potential_copy
potential_copy = [letter for letter in potential]
for letter in potential:
if letter not in sacks[2]:
# print(letter)
potential_copy.remove(letter)
print(potential)
return potential[0]
def main(filename):
sacks = ParseInput(filename)
total_priority = 0
for sack in sacks:
total_priority += CalculatePriority(sack)
group = []
total_group_priority = 0
for idx, sack in enumerate(sacks):
if idx % 3 != 0 or idx == 0:
group.append(sack)
else:
total_group_priority += LetterToPriority(FindGroups(group))
group = [sack]
total_group_priority += LetterToPriority(FindGroups([sack for sack in sacks[-3:]]))
print(total_priority)
print(total_group_priority)
if __name__ == "__main__":
main(sys.argv[1])