From 28e615f4a8e77099d81d6ecedd29f1a9d907d2bf Mon Sep 17 00:00:00 2001 From: Amneesh Singh Date: Sat, 21 Dec 2024 22:10:17 +0530 Subject: [PATCH] day 21: wow way faster now Signed-off-by: Amneesh Singh --- src/Day21.hs | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/src/Day21.hs b/src/Day21.hs index 7b9b124..302728f 100644 --- a/src/Day21.hs +++ b/src/Day21.hs @@ -1,16 +1,13 @@ module Main where -import Data.List (foldl', minimumBy, nub) +import Data.List (foldl', nub) import qualified Data.Map as M -import Data.Ord (comparing) - --- TODO: ugloid and slow solution, please clean type Vec = (Int, Int) type Table = M.Map (Char, Char) [String] -type Memo = M.Map (Char, Char, Int) Int +type Memo = M.Map (Int, Char, Char) Int coord :: Char -> Vec -- keypad @@ -52,15 +49,16 @@ seqc table n = fst . seqc' M.empty (n + 1) 'A' seqc' memo n start s = foldl' loop (0, memo) $ zip (start : s) s where loop :: (Int, Memo) -> (Char, Char) -> (Int, Memo) - loop (total, memo) (a, b) = case (a, b, n) `M.lookup` memo of + loop (total, memo) (a, b) = case (n, a, b) `M.lookup` memo of Just x -> (total + x, memo) Nothing -> - let paths = table M.! (a, b) - (x, memo') = - minimumBy - (comparing fst) - (map (seqc' memo (n - 1) 'a') paths) - in (total + x, M.insert (a, b, n) x memo') + let (x, memo') = case table M.! (a, b) of + [p] -> seqc' memo (n - 1) 'a' p + [p1, p2] -> + let (x1, m1) = seqc' memo (n - 1) 'a' p1 + (x2, m2) = seqc' m1 (n - 1) 'a' p2 + in if x1 < x2 then (x1, m2) else (x2, m2) + in (total + x, M.insert (n, a, b) x memo') complexity :: Table -> Int -> String -> Int complexity table n s = a * b