day 16: naive solution that obviously doesn’t work

This commit is contained in:
Gattix 2022-12-16 07:51:33 +01:00
parent 74fe38337f
commit 0624234733

32
2022/day16/day16.hs Normal file
View File

@ -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