1
0
Fork 0

add 2022/day03

This commit is contained in:
Sebastian Mark 2024-12-11 15:57:39 +01:00
parent 968207eb83
commit f555e978ca

47
2022/03/main.py Normal file
View file

@ -0,0 +1,47 @@
#!/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()