day 21: wow way faster now
Signed-off-by: Amneesh Singh <natto@weirdnatto.in>
This commit is contained in:
22
src/Day21.hs
22
src/Day21.hs
@@ -1,16 +1,13 @@
|
|||||||
module Main where
|
module Main where
|
||||||
|
|
||||||
import Data.List (foldl', minimumBy, nub)
|
import Data.List (foldl', nub)
|
||||||
import qualified Data.Map as M
|
import qualified Data.Map as M
|
||||||
import Data.Ord (comparing)
|
|
||||||
|
|
||||||
-- TODO: ugloid and slow solution, please clean
|
|
||||||
|
|
||||||
type Vec = (Int, Int)
|
type Vec = (Int, Int)
|
||||||
|
|
||||||
type Table = M.Map (Char, Char) [String]
|
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
|
coord :: Char -> Vec
|
||||||
-- keypad
|
-- 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
|
seqc' memo n start s = foldl' loop (0, memo) $ zip (start : s) s
|
||||||
where
|
where
|
||||||
loop :: (Int, Memo) -> (Char, Char) -> (Int, Memo)
|
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)
|
Just x -> (total + x, memo)
|
||||||
Nothing ->
|
Nothing ->
|
||||||
let paths = table M.! (a, b)
|
let (x, memo') = case table M.! (a, b) of
|
||||||
(x, memo') =
|
[p] -> seqc' memo (n - 1) 'a' p
|
||||||
minimumBy
|
[p1, p2] ->
|
||||||
(comparing fst)
|
let (x1, m1) = seqc' memo (n - 1) 'a' p1
|
||||||
(map (seqc' memo (n - 1) 'a') paths)
|
(x2, m2) = seqc' m1 (n - 1) 'a' p2
|
||||||
in (total + x, M.insert (a, b, n) x memo')
|
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 -> Int -> String -> Int
|
||||||
complexity table n s = a * b
|
complexity table n s = a * b
|
||||||
|
Reference in New Issue
Block a user