DEV Community

Discussion on: Daily Challenge #62 - Josephus Survivor

Collapse
 
avalander profile image
Avalander • Edited

Wow, this one was hard to figure out.

Haskell

survivor :: Int -> Int -> Int
survivor k step =
  survivor' step [ 1 .. k ]
  where
    survivor' :: Int -> [Int] -> Int
    survivor' step [x] = x
    survivor' step xs  = survivor' step (remove_at step' xs)
      where
        step' = reduce_step (length xs) step

    reduce_step :: Int -> Int -> Int
    reduce_step len step
      | step <= len = step
      | otherwise   = reduce_step len (step - len)

    remove_at :: Int -> [Int] -> [Int]
    remove_at i xs = (drop i xs) ++ (take (i - 1) xs)