Co-authored-by: Mikael CAPELLE <mikael.capelle@thalesaleniaspace.com> Co-authored-by: Mikaël Capelle <capelle.mikael@gmail.com> Reviewed-on: #3
29 lines
806 B
Python
29 lines
806 B
Python
import string
|
|
from typing import Any, Iterator
|
|
|
|
from ..base import BaseSolver
|
|
|
|
|
|
class Solver(BaseSolver):
|
|
def solve(self, input: str) -> Iterator[Any]:
|
|
lines = [line.strip() for line in input.splitlines()]
|
|
|
|
# extract content of each part
|
|
parts = [
|
|
(set(line[: len(line) // 2]), set(line[len(line) // 2 :])) for line in lines
|
|
]
|
|
|
|
# priorities
|
|
priorities = {c: i + 1 for i, c in enumerate(string.ascii_letters)}
|
|
|
|
# part 1
|
|
yield sum(priorities[c] for p1, p2 in parts for c in p1.intersection(p2))
|
|
|
|
# part 2
|
|
n_per_group = 3
|
|
yield sum(
|
|
priorities[c]
|
|
for i in range(0, len(lines), n_per_group)
|
|
for c in set(lines[i]).intersection(*lines[i + 1 : i + n_per_group])
|
|
)
|