2022 Day 3 Python (practically cheating for leaderboard)

This commit is contained in:
Luke Hubmayer-Werner 2022-12-03 15:41:07 +10:30
parent 04ab7a2c55
commit c7f407ea64
1 changed files with 29 additions and 0 deletions

29
2022/day3.py Normal file
View File

@ -0,0 +1,29 @@
with open('day3-input', 'r') as file:
input = file.read().strip().split('\n')
def prio(character):
if 'a' <= character <= 'z':
return ord(character) - ord('a') + 1
return ord(character) - ord('A') + 27
s = 0
for line in input:
l = len(line)//2
left = set(line[:l])
right = set(line[l:])
common = left & right
character = list(common)[0]
s += prio(character)
print(s) # Part 1 answer
s = 0
for group in range(len(input)//3):
e1 = set(input[group*3])
e2 = set(input[group*3+1])
e3 = set(input[group*3+2])
common = e1 & e2 & e3
character = list(common)[0]
s += prio(character)
print(s) # Part 2 answer