aoc2022/20-a.hs

36 lines
891 B
Haskell

import Common
import Data.List (elemIndex)
import Data.Maybe (fromJust)
wrap :: Int -> Int -> Int
wrap length index
| index == 0 = length - 1
| index == length - 1 = 0
| index >= length = wrap length $ index - length
| index < 0 = wrap length $ index + length
| otherwise = index
coordinates = [1000, 2000, 3000]
decrypt :: [Int] -> Int
decrypt a = sum $ map test coordinates
where
test n = mixed !! (zeroIndex + n)
zeroIndex = fromJust $ elemIndex 0 mixed
mixed = cycle $ foldl mix a a
len = length a
mix :: [Int] -> Int -> [Int]
mix list x = newList
where
newList = listStart ++ x : listEnd
(listStart, listEnd) = splitAt newIndex list'
newIndex = wrap (len - 1) $ oldIndex + x
list' = filter (/= x) list
oldIndex = fromJust $ elemIndex x list
main = interact $
show
. decrypt
. map read . lines