2016: d19: ex1: add solution

This commit is contained in:
Bruno BELANYI 2025-05-19 22:59:58 +01:00
parent 507279b0e9
commit 4dcbc0f431

25
2016/d19/ex1/ex1.py Executable file
View 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()