day11: hindent
This commit is contained in:
parent
930c6e0745
commit
2721636c9e
|
@ -1,8 +1,9 @@
|
||||||
{-# LANGUAGE TemplateHaskell #-}
|
{-# LANGUAGE TemplateHaskell #-}
|
||||||
|
|
||||||
import Control.Lens
|
import Control.Lens
|
||||||
|
import Data.List
|
||||||
import Data.List.Split
|
import Data.List.Split
|
||||||
import Data.Maybe
|
import Data.Maybe
|
||||||
import Data.List
|
|
||||||
|
|
||||||
data Monkey =
|
data Monkey =
|
||||||
Monkey
|
Monkey
|
||||||
|
@ -13,6 +14,7 @@ data Monkey =
|
||||||
, nextMonkey :: (Int, Int)
|
, nextMonkey :: (Int, Int)
|
||||||
, _inspectCounter :: Int
|
, _inspectCounter :: Int
|
||||||
}
|
}
|
||||||
|
|
||||||
makeLenses ''Monkey
|
makeLenses ''Monkey
|
||||||
|
|
||||||
instance Show Monkey where
|
instance Show Monkey where
|
||||||
|
@ -40,7 +42,8 @@ main = do
|
||||||
|
|
||||||
day11 :: Int -> (Int -> Int) -> [Monkey] -> Int
|
day11 :: Int -> (Int -> Int) -> [Monkey] -> Int
|
||||||
day11 x f m = g $ take (succ x) $ iterate (turns f) m
|
day11 x f m = g $ take (succ x) $ iterate (turns f) m
|
||||||
where g = product . take 2 . reverse . sort . map _inspectCounter . last
|
where
|
||||||
|
g = product . take 2 . reverse . sort . map _inspectCounter . last
|
||||||
|
|
||||||
parseMonkey :: [[String]] -> Maybe Monkey
|
parseMonkey :: [[String]] -> Maybe Monkey
|
||||||
parseMonkey [[a, _], [_, b], [_, c], [_, d], [_, e], [_, f]] =
|
parseMonkey [[a, _], [_, b], [_, c], [_, d], [_, e], [_, f]] =
|
||||||
|
@ -55,23 +58,36 @@ parseMonkey [[a, _], [_, b], [_, c], [_, d], [_, e], [_, f]] =
|
||||||
where
|
where
|
||||||
f' = read . last . words
|
f' = read . last . words
|
||||||
parseOP [_, _, _, x, y]
|
parseOP [_, _, _, x, y]
|
||||||
| y == "old" = (^ (2::Int))
|
| y == "old" = (^ (2 :: Int))
|
||||||
| x == "*" = (*) (read y)
|
| x == "*" = (*) (read y)
|
||||||
| otherwise = (+) (read y)
|
| otherwise = (+) (read y)
|
||||||
parseMonkey _ = Nothing
|
parseMonkey _ = Nothing
|
||||||
|
|
||||||
turns :: (Int -> Int) -> [Monkey] -> [Monkey]
|
turns :: (Int -> Int) -> [Monkey] -> [Monkey]
|
||||||
turns f = turns' 0
|
turns f = turns' 0
|
||||||
where turns' n ms
|
where
|
||||||
| length ms == n = ms
|
turns' n ms
|
||||||
| otherwise = turns' (succ n) monkeNew
|
| length ms == n = ms
|
||||||
where itemsNew = [ f $ flip mod modNum $ operation (ms !! n) x | x<-_items (ms !! n)]
|
| otherwise = turns' (succ n) monkeNew
|
||||||
monkeEmptied = ms & ix n .~ ((ms !! n) & items .~ [] & inspectCounter +~ length itemsNew)
|
where
|
||||||
monkeNew = monkeThrow itemsNew n monkeEmptied
|
itemsNew =
|
||||||
modNum = foldl1 lcm $ map test ms
|
[f $ flip mod modNum $ operation (ms !! n) x | x <- _items (ms !! n)]
|
||||||
|
monkeEmptied =
|
||||||
|
ms &
|
||||||
|
ix n .~ ((ms !! n) & items .~ [] & inspectCounter +~ length itemsNew)
|
||||||
|
monkeNew = monkeThrow itemsNew n monkeEmptied
|
||||||
|
modNum = foldl1 lcm $ map test ms
|
||||||
|
|
||||||
monkeThrow :: [Int] -> Int -> [Monkey] -> [Monkey]
|
monkeThrow :: [Int] -> Int -> [Monkey] -> [Monkey]
|
||||||
monkeThrow (x:xs) n ms = monkeThrow xs n (ms & ix next .~ ((ms !! next) & items .~ (_items (ms !! next) ++ [x]) ))
|
monkeThrow (x:xs) n ms =
|
||||||
where next = if mod x (test (ms !! n)) == 0 then fst targets else snd targets
|
monkeThrow
|
||||||
targets = nextMonkey (ms !! n)
|
xs
|
||||||
|
n
|
||||||
|
(ms & ix next .~ ((ms !! next) & items .~ (_items (ms !! next) ++ [x])))
|
||||||
|
where
|
||||||
|
next =
|
||||||
|
if mod x (test (ms !! n)) == 0
|
||||||
|
then fst targets
|
||||||
|
else snd targets
|
||||||
|
targets = nextMonkey (ms !! n)
|
||||||
monkeThrow _ _ ms = ms
|
monkeThrow _ _ ms = ms
|
||||||
|
|
Loading…
Reference in New Issue
Block a user