-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.hs
30 lines (25 loc) · 849 Bytes
/
solution.hs
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
import Data.List
import Data.Maybe
prsInt :: String -> Int
prsInt = read
redist :: [Int] -> [Int]
redist xs =
[ if x == start then 0 + inc else if rest >= (mod (x - start) ln) then (xs !! x) + inc + 1 else (xs !! x) + inc |
x <- [0..length xs - 1],
let ln = length xs,
let m = maximum xs,
let inc = div m ln,
let rest = mod m ln,
let start = fromJust (elemIndex m xs)
]
next :: [[Int]] -> Int -> [[Int]]
next list cycles
| elem (head list) (tail list) = list
| otherwise = next ([redist (head list)] ++ list) (cycles + 1)
part1 = do
input <- return . map prsInt . words =<< readFile "input"
return $ length (next [input] 0) - 1
part2 = do
input <- return . map prsInt . words =<< readFile "input"
let list = next [input] 0
return $ fromJust (elemIndex (head list) (drop 1 list)) + 1