-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.py
49 lines (37 loc) · 1.16 KB
/
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
import pathlib
import string
def getInput(path):
input = pathlib.Path(path).read_text().splitlines()
return input
def part1(input):
priorities = dict(zip(string.ascii_letters, range(1, 53)))
total = 0
for sack in input:
checklist = set()
endSack = len(sack)
halfSack = int(endSack / 2)
for i in range(0, halfSack):
item = sack[i]
checklist.add(item)
for i in range(halfSack, endSack):
item = sack[i]
if item in checklist:
total += priorities[item]
break
return total
def part2(input):
priorities = dict(zip(string.ascii_letters, range(1, 53)))
total = 0
for i in range(0, len(input), 3):
group = input[i : i + 3]
member0, member1, member2 = set(group[0]), set(group[1]), set(group[2])
badge = list(member0.intersection(member1).intersection(member2))[0]
total += priorities[badge]
return total
def main():
input = getInput("input.txt")
print(f"Answer to part 1: {part1(input)}")
print(f"Answer to part 2: {part2(input)}")
return
if __name__ == "__main__":
main()