diff --git a/2022/day16/day16.hs b/2022/day16/day16.hs new file mode 100644 index 0000000..e5f25f0 --- /dev/null +++ b/2022/day16/day16.hs @@ -0,0 +1,32 @@ +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