diff --git a/2021/day12.hs b/2021/day12.hs index 0a721f7..875ab4f 100644 --- a/2021/day12.hs +++ b/2021/day12.hs @@ -1,18 +1,11 @@ module Day12 where --- import Data.HashSet (HashSet, member, insert, fromList) --- import Data.Map ( Map, fromList, unionWith, empty, (!), insert, elems) qualified (map) -import qualified Data.Map as M import Data.Bits ( Bits(shiftL, (.|.), (.&.)) ) +import Data.Char ( isUpper ) +import Data.List.Split (splitOn) +import qualified Data.Map as M import qualified Data.Array as A -import Data.HashSet (HashSet) -import qualified Data.HashSet as S -import Data.List.Split (splitOn) -import qualified Data.Char - -import Debug.Trace ( trace ) -import GHC.Base (VecElem(Int16ElemRep)) type Cave = Int type Path = (Cave,Cave) @@ -25,8 +18,8 @@ readPath s = (from,to) where [from,to] = splitOn "-" s addPath :: (String,String) -> M.Map String [String] -> M.Map String [String] addPath (from,to) = M.unionWith (++) (M.fromList [(from,[to]), (to,[from])]) -isUpper :: Cave -> Bool -isUpper x = x < 0 +isBig :: Cave -> Bool +isBig x = x < 0 solve :: System -> (Int, Int) solve sys = (pf1a A.! (1,1), pf2a A.! (1, 1)) @@ -40,8 +33,8 @@ solve sys = (pf1a A.! (1,1), pf2a A.! (1, 1)) pf1 (-1) _ = 1 pf1 x visited = sum $ map (\next -> pf1a A.! (next,nextmap next)) elig where - nextmap next = if not (isUpper next) then next .|. visited else visited - elig = filter (\cave -> isUpper cave || (cave .&. visited) == 0) (sys M.! x) + nextmap next = if not (isBig next) then next .|. visited else visited + elig = filter (\cave -> isBig cave || (cave .&. visited) == 0) (sys M.! x) pf2a :: A.Array (Cave,Int) Int pf2a = A.array bounds [((c,v), pf2 c v) | c <- [minimum caves..maximum caves], v <- [0..maximum caves * 2] ] @@ -49,8 +42,8 @@ solve sys = (pf1a A.! (1,1), pf2a A.! (1, 1)) pf2 (-1) _ = 1 pf2 x visited = sum $ map (\next -> f next A.! (next,nextmap next)) elig where - nextmap next = if not (isUpper next) then next .|. visited else visited - f cave = if isUpper cave || (cave .&. visited) == 0 then pf2a else pf1a + nextmap next = if not (isBig next) then next .|. visited else visited + f cave = if isBig cave || (cave .&. visited) == 0 then pf2a else pf1a elig = filter (/= 1) (sys M.! x) convert :: [String] -> M.Map String Int