add 2022/day03
This commit is contained in:
parent
968207eb83
commit
f555e978ca
1 changed files with 47 additions and 0 deletions
47
2022/03/main.py
Normal file
47
2022/03/main.py
Normal 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()
|
Loading…
Reference in a new issue