From 062423473386a4d71841698a1d70934803d7e9f9 Mon Sep 17 00:00:00 2001 From: Gattix Date: Fri, 16 Dec 2022 07:51:33 +0100 Subject: [PATCH] =?UTF-8?q?day=2016:=20naive=20solution=20that=20obviously?= =?UTF-8?q?=20doesn=E2=80=99t=20work?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 2022/day16/day16.hs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 2022/day16/day16.hs 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