1
0
Fork 0

Day 6, updated for part 2

This commit is contained in:
Jonathan Chan 2017-12-12 09:28:47 -08:00
parent 9052fd9db6
commit 2b209648d5
1 changed files with 7 additions and 7 deletions

14
6.hs
View File

@ -1,9 +1,9 @@
import Data.Foldable
import Data.HashSet
import Data.HashMap
import Data.Sequence
type Bank = Seq Int
type Config = (Bank, Data.HashSet.HashSet [Int])
type Config = (Bank, Data.HashMap.Map [Int] Int)
(%) :: Int -> Int -> Int
(%) = mod
@ -16,22 +16,22 @@ nextBank :: Bank -> Bank
nextBank bank =
let len = Data.Sequence.length bank
(index, value) = getMaxMem bank
zeroedBank = update index 0 bank
zeroedBank = Data.Sequence.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 :: Int -> Config -> (Int, 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)
then (count + 1, (count + 1) - findWithDefault undefined hashableBank configs)
else cycles (count + 1) (bank, insert hashableBank (count + 1) 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)
print $ cycles 0 (bank, Data.HashMap.empty)