2018: d01: ex2: add solution
This commit is contained in:
parent
53c6dcdb2a
commit
1cf545aa1a
31
2018/d01/ex2/ex2.py
Executable file
31
2018/d01/ex2/ex2.py
Executable file
|
@ -0,0 +1,31 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
import itertools
|
||||
import sys
|
||||
|
||||
|
||||
def solve(input: str) -> int:
|
||||
def parse(input: list[str]) -> list[int]:
|
||||
return [int(n) for n in input]
|
||||
|
||||
def find_duplicate(deltas: list[int]) -> int:
|
||||
start = 0
|
||||
seen = {start}
|
||||
for delta in itertools.cycle(deltas):
|
||||
start += delta
|
||||
if start in seen:
|
||||
return start
|
||||
seen.add(start)
|
||||
assert False # Sanity check
|
||||
|
||||
deltas = parse(input.splitlines())
|
||||
return find_duplicate(deltas)
|
||||
|
||||
|
||||
def main() -> None:
|
||||
input = sys.stdin.read()
|
||||
print(solve(input))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
Loading…
Reference in a new issue