2022: d03: ex2: add solution
This commit is contained in:
parent
035b00e5cf
commit
3030162e09
29
2022/d03/ex2/ex2.py
Executable file
29
2022/d03/ex2/ex2.py
Executable file
|
@ -0,0 +1,29 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
import sys
|
||||
from collections.abc import Iterator
|
||||
|
||||
Group = tuple[str, str, str]
|
||||
|
||||
|
||||
def solve(input: list[str]) -> int:
|
||||
def score(lines: Group) -> int:
|
||||
common_items = set(lines[0]) & set(lines[1]) & set(lines[2])
|
||||
assert len(common_items) == 1 # Sanity check
|
||||
common = common_items.pop()
|
||||
return common.isupper() * 26 + ord(common.lower()) - ord("a") + 1
|
||||
|
||||
def iter_3() -> Iterator[Group]:
|
||||
args = [iter(input)] * 3
|
||||
yield from zip(*args, strict=True) # type: ignore
|
||||
|
||||
return sum(map(score, iter_3()))
|
||||
|
||||
|
||||
def main() -> None:
|
||||
input = sys.stdin.read().splitlines()
|
||||
print(solve(input))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
Loading…
Reference in a new issue