1
0
Fork 0

Day 6, part 1

This commit is contained in:
Jonathan Chan 2017-12-12 09:28:43 -08:00
parent 507a271dab
commit 9052fd9db6
2 changed files with 38 additions and 0 deletions

37
6.hs Normal file
View File

@ -0,0 +1,37 @@
import Data.Foldable
import Data.HashSet
import Data.Sequence
type Bank = Seq Int
type Config = (Bank, Data.HashSet.HashSet [Int])
(%) :: Int -> Int -> Int
(%) = mod
getMaxMem :: Bank -> (Int, Int)
getMaxMem bank =
foldlWithIndex (\(currIndex, currMax) index value -> if value > currMax then (index, value) else (currIndex, currMax)) (0, 0) bank
nextBank :: Bank -> Bank
nextBank bank =
let len = Data.Sequence.length bank
(index, value) = getMaxMem bank
zeroedBank = update index 0 bank
mappedBank = fmap (+ value `div` len) zeroedBank
indicesToUpdate = fmap (% len) [(index + 1)..(index + value % len)]
in mapWithIndex (\i v -> if i `elem` indicesToUpdate then v + 1 else v) mappedBank
cycles :: Int -> Config -> Int
cycles count (prevBank, configs) =
let bank = nextBank prevBank
hashableBank = Data.Foldable.toList bank
in if member hashableBank configs
then count + 1
else cycles (count + 1) (bank, insert hashableBank configs)
main :: IO ()
main = do
input <- readFile "6.txt"
let bank = Data.Sequence.fromList $ fmap read $ words input :: Bank
print $ cycles 0 (bank, Data.HashSet.empty)

1
6.txt Normal file
View File

@ -0,0 +1 @@
14 0 15 12 11 11 3 5 1 6 8 4 9 1 8 4