78 lines
1.4 KiB
Python
78 lines
1.4 KiB
Python
#!/usr/bin/env python3
|
|
|
|
|
|
from typing import TextIO
|
|
|
|
ltrs = "abcdefghijklmnopqrstuvwxyz"
|
|
# lowercase: pos + 1
|
|
# uppercase: pos + 27
|
|
|
|
|
|
def find_common_char(one: str, two: str) -> str:
|
|
for a in one:
|
|
for b in two:
|
|
if a == b:
|
|
return a
|
|
|
|
return ""
|
|
|
|
|
|
def find_common_char_three(one: str, two: str, three: str) -> str:
|
|
for a in one:
|
|
for b in two:
|
|
for c in three:
|
|
if a == b == c:
|
|
return a
|
|
|
|
return ""
|
|
|
|
|
|
def get_priority(ltr: str) -> int:
|
|
|
|
n = ltrs.find(ltr.lower())
|
|
|
|
if ltr.isupper():
|
|
return n + 27
|
|
|
|
return n + 1
|
|
|
|
|
|
def part1(sacks: TextIO) -> int:
|
|
|
|
sum: int = 0
|
|
for sack in sacks:
|
|
first = sack[: len(sack) // 2]
|
|
second = sack[len(sack) // 2 :]
|
|
|
|
sum += get_priority(find_common_char(first, second))
|
|
|
|
return sum
|
|
|
|
|
|
def part2(sacks: TextIO) -> int:
|
|
sum: int = 0
|
|
|
|
line = sacks.readline().strip()
|
|
|
|
while line != "":
|
|
a, b, c = "", "", ""
|
|
|
|
for i in range(0, 3):
|
|
if line != "": # lol
|
|
a = line
|
|
line = sacks.readline().strip()
|
|
b = line
|
|
line = sacks.readline().strip()
|
|
c = line
|
|
|
|
line = sacks.readline().strip()
|
|
|
|
sum += get_priority((find_common_char_three(a, b, c)))
|
|
|
|
return sum
|
|
|
|
|
|
if __name__ == "__main__":
|
|
print(part1(open("day03.txt")))
|
|
print(part2(open("day03.txt")))
|