-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay20.hs
executable file
·37 lines (31 loc) · 1.04 KB
/
Day20.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
31
32
33
34
35
36
37
#!/usr/bin/env stack
{-
stack script
--resolver lts-20.2
-}
rotateToElem :: Eq a => a -> [(a, Int)] -> [(a, Int)]
rotateToElem v ls =
let (b, a) = span ((/=) v . fst) ls
in a ++ b
rotateToInd :: Int -> [(a, Int)] -> [(a, Int)]
rotateToInd i ls =
let (b, a) = span ((/=) i . snd) ls
in a ++ b
update1 :: [(Int, Int)] -> [(Int, Int)]
update1 [] = []
update1 (h@(v, _):t) =
let (a, b) = splitAt (v `mod` length t) t
in a ++ (h:b)
mix :: [(Int, Int)] -> [(Int, Int)]
mix toMix = foldl (\l' i -> update1 $ rotateToInd i l') toMix [0..length toMix - 1]
getCircular :: [a] -> Int -> a
getCircular l p = l!!(p `mod` length l)
main :: IO ()
main = do
input <- readFile "data/20.txt"
let list = flip zip [0..] . fmap (\v -> read v :: Int) . lines $ input
let mixed = rotateToElem 0 $ mix list
print . sum . fmap (fst . getCircular mixed) $ [1000, 2000, 3000]
let updated = (\(v, i) -> (v*811589153,i)) <$> list
let mixed' = rotateToElem 0 . (!! 10) . iterate mix $ updated
print . sum . fmap (fst . getCircular mixed') $ [1000, 2000, 3000]