full memoization
This commit is contained in:
parent
7f8ce383f9
commit
0721d09f5a
|
|
@ -4,11 +4,16 @@ module Day12 where
|
||||||
import qualified Data.Map as M
|
import qualified Data.Map as M
|
||||||
import Data.Bits ( Bits(shiftL, (.|.), (.&.)) )
|
import Data.Bits ( Bits(shiftL, (.|.), (.&.)) )
|
||||||
|
|
||||||
|
import qualified Data.Array as A
|
||||||
|
|
||||||
import Data.HashSet (HashSet)
|
import Data.HashSet (HashSet)
|
||||||
import qualified Data.HashSet as S
|
import qualified Data.HashSet as S
|
||||||
import Data.List.Split (splitOn)
|
import Data.List.Split (splitOn)
|
||||||
import qualified Data.Char
|
import qualified Data.Char
|
||||||
|
|
||||||
|
import Debug.Trace ( trace )
|
||||||
|
import GHC.Base (VecElem(Int16ElemRep))
|
||||||
|
|
||||||
type Cave = Int
|
type Cave = Int
|
||||||
type Path = (Cave,Cave)
|
type Path = (Cave,Cave)
|
||||||
type System = M.Map Cave [Cave]
|
type System = M.Map Cave [Cave]
|
||||||
|
|
@ -23,20 +28,30 @@ addPath (from,to) = M.unionWith (++) (M.fromList [(from,[to]), (to,[from])])
|
||||||
isUpper :: Cave -> Bool
|
isUpper :: Cave -> Bool
|
||||||
isUpper x = x < 0
|
isUpper x = x < 0
|
||||||
|
|
||||||
pathFinder :: System -> Cave -> Int -> Int
|
solve :: System -> (Int, Int)
|
||||||
pathFinder sys (-1) _ = 1
|
solve sys = (pf1a A.! (1,1), pf2a A.! (1, 1))
|
||||||
pathFinder sys x visited = sum $ map (\next -> pathFinder sys next (nextmap next)) elig
|
|
||||||
where
|
where
|
||||||
nextmap next = if not (isUpper next) then next .|. visited else visited
|
caves = concat $ M.elems sys
|
||||||
elig = filter (\cave -> isUpper cave || (cave .&. visited) == 0) (sys M.! x)
|
bounds = ((minimum caves, 0), (maximum caves, maximum caves * 2))
|
||||||
|
|
||||||
pathFinder2 :: System -> Cave -> Int -> Int
|
pf1a :: A.Array (Cave,Int) Int
|
||||||
pathFinder2 sys (-1) _ = 1
|
pf1a = A.array bounds [((c,v), pf1 c v) | c <- [minimum caves..maximum caves], v <- [0..maximum caves * 2] ]
|
||||||
pathFinder2 sys x visited = sum $ map (\next -> f next sys next (nextmap next)) elig
|
pf1 :: Cave -> Int -> Int
|
||||||
where
|
pf1 (-1) _ = 1
|
||||||
nextmap next = if not (isUpper next) then next .|. visited else visited
|
pf1 x visited = sum $ map (\next -> pf1a A.! (next,nextmap next)) elig
|
||||||
f cave = if isUpper cave || (cave .&. visited) == 0 then pathFinder2 else pathFinder
|
where
|
||||||
elig = filter (/= 1) (sys M.! x)
|
nextmap next = if not (isUpper next) then next .|. visited else visited
|
||||||
|
elig = filter (\cave -> isUpper 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] ]
|
||||||
|
pf2 :: Cave -> Int -> Int
|
||||||
|
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
|
||||||
|
elig = filter (/= 1) (sys M.! x)
|
||||||
|
|
||||||
convert :: [String] -> M.Map String Int
|
convert :: [String] -> M.Map String Int
|
||||||
convert caves = conversion
|
convert caves = conversion
|
||||||
|
|
@ -52,11 +67,12 @@ main = do
|
||||||
input <- map readPath . lines <$> getContents
|
input <- map readPath . lines <$> getContents
|
||||||
let system = foldr addPath M.empty input
|
let system = foldr addPath M.empty input
|
||||||
let conversion = convert $ concat $ M.elems system
|
let conversion = convert $ concat $ M.elems system
|
||||||
print conversion
|
|
||||||
let integralSystem = M.mapKeys (conversion M.!) $ M.map (map (conversion M.!)) system
|
let integralSystem = M.mapKeys (conversion M.!) $ M.map (map (conversion M.!)) system
|
||||||
|
|
||||||
|
let (p1,p2) = solve integralSystem
|
||||||
|
|
||||||
putStrLn "part 1: "
|
putStrLn "part 1: "
|
||||||
print $ pathFinder integralSystem 1 1
|
print p1
|
||||||
|
|
||||||
putStrLn "part 2: "
|
putStrLn "part 2: "
|
||||||
print $ pathFinder2 integralSystem 1 1
|
print p2
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue