1
0
Fork 0

Day 6 - imported only needed functions so that I don't have to Data.* everywhere

This commit is contained in:
Jonathan Chan 2017-12-12 09:28:55 -08:00
parent cf61ef2a44
commit 40c2d06a8c
1 changed files with 8 additions and 8 deletions

16
6.hs
View File

@ -1,10 +1,10 @@
import Data.Foldable import Data.Foldable (toList)
import Data.HashMap import Data.HashMap (Map, member, insert, findWithDefault, empty)
import Data.Sequence import Data.Sequence (Seq, length, update, fromList, foldlWithIndex, mapWithIndex)
type Bank = Seq Int type Bank = Seq Int
type HashableBank = [Int] type HashableBank = [Int]
type Config = (Bank, Data.HashMap.Map HashableBank Int) type Config = (Bank, Map HashableBank Int)
(%) :: Int -> Int -> Int (%) :: Int -> Int -> Int
(%) = mod (%) = mod
@ -20,7 +20,7 @@ nextBank :: Bank -> Bank
nextBank bank = nextBank bank =
let len = Data.Sequence.length bank let len = Data.Sequence.length bank
(index, value) = getMaxMem bank (index, value) = getMaxMem bank
zeroedBank = Data.Sequence.update index 0 bank zeroedBank = update index 0 bank
mappedBank = fmap (+ value // len) zeroedBank mappedBank = fmap (+ value // len) zeroedBank
indicesToUpdate = fmap ((% len) . (+ index)) [1..value % len] indicesToUpdate = fmap ((% len) . (+ index)) [1..value % len]
in mapWithIndex (\i v -> if i `elem` indicesToUpdate then v + 1 else v) mappedBank in mapWithIndex (\i v -> if i `elem` indicesToUpdate then v + 1 else v) mappedBank
@ -30,7 +30,7 @@ cycles :: Int -> Config -> (Int, Int)
cycles prevCount (prevBank, banks) = cycles prevCount (prevBank, banks) =
let count = prevCount + 1 let count = prevCount + 1
bank = nextBank prevBank bank = nextBank prevBank
hashableBank = Data.Foldable.toList bank hashableBank = toList bank
in if member hashableBank banks in if member hashableBank banks
then (count, count - findWithDefault undefined hashableBank banks) then (count, count - findWithDefault undefined hashableBank banks)
else cycles count (bank, insert hashableBank count banks) else cycles count (bank, insert hashableBank count banks)
@ -38,5 +38,5 @@ cycles prevCount (prevBank, banks) =
main :: IO () main :: IO ()
main = do main = do
input <- readFile "6.txt" input <- readFile "6.txt"
let bank = Data.Sequence.fromList $ fmap read $ words input :: Bank let bank = fromList $ fmap read $ words input :: Bank
print $ cycles 0 (bank, Data.HashMap.empty) print $ cycles 0 (bank, empty)