-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.nim
52 lines (42 loc) · 837 Bytes
/
solution.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import std/[
algorithm,
bitops,
deques,
json,
math,
re,
sequtils,
sets,
strformat,
strutils,
tables,
sugar,
]
import ../../lib/llrb
proc joseph(n: int): int =
(n - (n.nextPowerOfTwo shr 1)) shl 1 + 1
when defined(test):
block:
doAssert joseph(5) == 3
proc part1(input: string): int =
let n = input.parseInt
joseph(n)
proc joseph2(n: int): int =
var ol = newOrderedList[int]()
for i in 1 .. n:
ol.insert i
while ol.len > 1:
for i in ol.items:
let j = (ol.find(i) + (ol.len shr 1)) mod ol.len
ol.deleteAt(j)
ol.getAt(0)
when defined(test):
block:
doAssert joseph2(5) == 2
proc part2(input: string): int =
let n = input.parseInt
joseph2(n)
when isMainModule and not defined(test):
let input = readAll(stdin).strip
echo part1(input)
echo part2(input)