2016: d19: ex1: add solution
This commit is contained in:
parent
507279b0e9
commit
4dcbc0f431
1 changed files with 25 additions and 0 deletions
25
2016/d19/ex1/ex1.py
Executable file
25
2016/d19/ex1/ex1.py
Executable file
|
|
@ -0,0 +1,25 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
import sys
|
||||||
|
|
||||||
|
|
||||||
|
def solve(input: str) -> int:
|
||||||
|
def msb(n: int) -> int:
|
||||||
|
assert n # Sanity check
|
||||||
|
return n.bit_length() - 1
|
||||||
|
|
||||||
|
# https://en.wikipedia.org/wiki/Josephus_problem
|
||||||
|
def josephus(n: int) -> int:
|
||||||
|
return 2 * (n - (1 << msb(n))) + 1
|
||||||
|
|
||||||
|
num_elves = int(input.strip())
|
||||||
|
return josephus(num_elves)
|
||||||
|
|
||||||
|
|
||||||
|
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