47 lines
1.2 KiB
Python
47 lines
1.2 KiB
Python
#!/usr/bin/env python
|
|
# -*- encoding: utf-8; py-indent-offset: 4 -*-
|
|
|
|
# Author: Sebastian Mark
|
|
# CC-BY-SA (https://creativecommons.org/licenses/by-sa/4.0/deed.de)
|
|
|
|
# pylint: disable=missing-module-docstring,missing-function-docstring,consider-using-f-string
|
|
|
|
|
|
def readinput():
|
|
with open("input", "r", encoding="utf-8") as file:
|
|
lines = [line.strip() for line in file.readlines()]
|
|
return lines
|
|
|
|
|
|
def charvalue(char: str) -> int:
|
|
values = "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
return values.index(char)
|
|
|
|
|
|
def main():
|
|
rucksacks = readinput()
|
|
|
|
# part 1
|
|
prios = 0
|
|
for rucksack in rucksacks:
|
|
middle = len(rucksack) // 2
|
|
left = rucksack[middle:]
|
|
right = rucksack[:middle]
|
|
common = set(left) & set(right)
|
|
for char in common:
|
|
prios += charvalue(char)
|
|
print("Rucksacks priorities: %d" % prios)
|
|
|
|
# part 2
|
|
prios = 0
|
|
i = 0
|
|
while i <= len(rucksacks) - 3:
|
|
common = set(rucksacks[i]) & set(rucksacks[i + 1]) & set(rucksacks[i + 2])
|
|
for char in common:
|
|
prios += charvalue(char)
|
|
i += 3
|
|
print("Group rucksacks priorities: %d" % prios)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|