diff --git a/2022/03/main.py b/2022/03/main.py new file mode 100644 index 0000000..03a0145 --- /dev/null +++ b/2022/03/main.py @@ -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()