aboutsummaryrefslogtreecommitdiff
path: root/Puzzle14.hs
diff options
context:
space:
mode:
authorYuchen Pei <me@ypei.me>2017-12-15 13:42:55 +0100
committerYuchen Pei <me@ypei.me>2017-12-15 13:42:55 +0100
commit8b79480eac1406524032cab6d0cc3bb5f3ab5e3f (patch)
treeb82bb342d71503ddfb9f85d0d7a7b98f74526125 /Puzzle14.hs
parent5dfee5ce272db6827aecd13bc2902be50dcf21b0 (diff)
finished day 14 part 2
Diffstat (limited to 'Puzzle14.hs')
-rw-r--r--Puzzle14.hs15
1 files changed, 14 insertions, 1 deletions
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)