AoC/2022/day16/day16.hs

33 lines
870 B
Haskell

import Control.Lens
import Data.List.Split
import qualified Data.Map as M
type Valves = M.Map String Node
type Node = (Int, [String])
main :: IO ()
main = do
input <- map words . lines <$> readFile "testinput"
print $ parse input
print $ walk 5 "AA" $ parse input
parse :: [[String]] -> Valves
parse = M.fromList . concatMap parse'
where
parse' (_:name:_:_:rate:_:_:_:_:ts) =
[(name, (0, ("open" ++ name) : nexts)), ("open" ++ name, (r, nexts))]
where
r = read $ init $ last $ splitOn "=" rate
nexts = map (\(x:y:_) -> [x, y]) ts
-- value multiplicator: 28, 25, etc
-- delete opened nodes (both)
walk :: Int -> String -> Valves -> Int
walk n start valves
| n == 0 = 0
| otherwise = n * rate + maximum [walk (pred n) x valves | x<-nextNodes]
where rate = valves M.! start ^._1
nextNodes = valves M.! start ^._2