From 8b79480eac1406524032cab6d0cc3bb5f3ab5e3f Mon Sep 17 00:00:00 2001 From: Yuchen Pei Date: Fri, 15 Dec 2017 13:42:55 +0100 Subject: finished day 14 part 2 --- Puzzle14.hs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'Puzzle14.hs') diff --git a/Puzzle14.hs b/Puzzle14.hs index d536c8d..c904d4a 100644 --- a/Puzzle14.hs +++ b/Puzzle14.hs @@ -1,6 +1,7 @@ import Puzzle10 (knotHash) import qualified Data.Map as Map import Data.Map (Map) +import Data.Maybe (catMaybes) -- import Data.Char (digit2Int) hex2Bin '0' = "0000" @@ -41,4 +42,16 @@ mat2Map xs = Map.fromList $ mconcat $ fmap f $ zipWith (,) [0 .. length xs - 1] step :: (Int, [(Int, Int)], Map (Int, Int) Bool) -> Int step (n, queue, mat) | null mat = n - | null queue = head $ Map.toList mat + | null queue = let (z, b) = head $ Map.toList mat in + if b then step (n + 1, [z], Map.delete z mat) + else step (n, [], Map.delete z mat) + | otherwise = step (n, catMaybes (zipWith f nbhd $ flip Map.lookup mat <$> nbhd) ++ zs, Map.delete (x, y) mat) + where (x, y):zs = queue + nbhd = [(x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1)] + +f :: (Int, Int) -> Maybe Bool -> Maybe (Int, Int) +f x (Just True) = Just x +f x _ = Nothing + +solve2 :: [Char] -> Int +solve2 xs = step (0, [], mat2Map $ truthMat xs) -- cgit v1.2.3