2015: d05: ex2: add solution
This commit is contained in:
parent
0cc937849a
commit
e3ed06e246
1 changed files with 42 additions and 0 deletions
42
2015/d05/ex2/ex2.py
Executable file
42
2015/d05/ex2/ex2.py
Executable file
|
|
@ -0,0 +1,42 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
import collections
|
||||
import itertools
|
||||
import sys
|
||||
from collections.abc import Iterable, Iterator
|
||||
from typing import TypeVar
|
||||
|
||||
T = TypeVar("T")
|
||||
|
||||
|
||||
def sliding_window(iterable: Iterable[T], n: int) -> Iterator[tuple[T, ...]]:
|
||||
iterator = iter(iterable)
|
||||
window = collections.deque(itertools.islice(iterator, n - 1), maxlen=n)
|
||||
for x in iterator:
|
||||
window.append(x)
|
||||
yield tuple(window)
|
||||
|
||||
|
||||
def solve(input: str) -> int:
|
||||
def is_nice(input: str) -> bool:
|
||||
if not any(
|
||||
ab in rest
|
||||
for ab, rest in (
|
||||
(input[i : i + 2], input[i + 2 :]) for i in range(len(input) - 1)
|
||||
)
|
||||
):
|
||||
return False
|
||||
if not any(a == c for a, _, c in sliding_window(input, 3)):
|
||||
return False
|
||||
return True
|
||||
|
||||
return sum(map(is_nice, input.splitlines()))
|
||||
|
||||
|
||||
def main() -> None:
|
||||
input = sys.stdin.read()
|
||||
print(solve(input))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Loading…
Add table
Add a link
Reference in a new issue