From 5dfee5ce272db6827aecd13bc2902be50dcf21b0 Mon Sep 17 00:00:00 2001 From: Yuchen Pei Date: Thu, 14 Dec 2017 16:32:31 +0100 Subject: finished Day 14 part 1 --- Puzzle10.hs | 6 +++++- Puzzle14.hs | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 Puzzle14.hs diff --git a/Puzzle10.hs b/Puzzle10.hs index fc01499..4af8105 100644 --- a/Puzzle10.hs +++ b/Puzzle10.hs @@ -1,3 +1,4 @@ +module Puzzle10 (knotHash) where import Data.List.Split (splitOn) import Data.Bits (xor) import Numeric (showHex) @@ -47,6 +48,9 @@ denseHash :: [Int] -> [Char] denseHash xs = mconcat $ fmap (int2Hex . fst) $ take 16 $ drop 1 $ iterate stepHash (0, xs) solve2 :: [Char] -solve2 = denseHash $ sparseHash $ (ord <$> input) ++ [17, 31, 73, 47, 23] +solve2 = knotHash input + +knotHash :: [Char] -> [Char] +knotHash xs = denseHash $ sparseHash $ (ord <$> xs) ++ [17, 31, 73, 47, 23] input = "120,93,0,90,5,80,129,74,1,165,204,255,254,2,50,113" diff --git a/Puzzle14.hs b/Puzzle14.hs new file mode 100644 index 0000000..d536c8d --- /dev/null +++ b/Puzzle14.hs @@ -0,0 +1,44 @@ +import Puzzle10 (knotHash) +import qualified Data.Map as Map +import Data.Map (Map) +-- import Data.Char (digit2Int) + +hex2Bin '0' = "0000" +hex2Bin '1' = "0001" +hex2Bin '2' = "0010" +hex2Bin '3' = "0011" +hex2Bin '4' = "0100" +hex2Bin '5' = "0101" +hex2Bin '6' = "0110" +hex2Bin '7' = "0111" +hex2Bin '8' = "1000" +hex2Bin '9' = "1001" +hex2Bin 'a' = "1010" +hex2Bin 'b' = "1011" +hex2Bin 'c' = "1100" +hex2Bin 'd' = "1101" +hex2Bin 'e' = "1110" +hex2Bin 'f' = "1111" + +matrix :: [Char] -> [[Char]] +matrix xs = (mconcat . (fmap hex2Bin) . knotHash . (\x -> xs ++ "-" ++ (show x))) <$> [0 .. 127] + +solve1 :: [Char] -> Int +solve1 = length . (filter (=='1')) . mconcat . matrix + +i2B :: Char -> Bool +i2B '0' = False +i2B '1' = True + + +truthMat :: [Char] -> [[Bool]] +truthMat = fmap (fmap i2B) . matrix + +mat2Map :: [[a]] -> Map (Int, Int) a +mat2Map xs = Map.fromList $ mconcat $ fmap f $ zipWith (,) [0 .. length xs - 1] xs + where f (x, ys) = zipWith (,) ((\y -> (x,y)) <$> [0 .. length ys]) ys + +step :: (Int, [(Int, Int)], Map (Int, Int) Bool) -> Int +step (n, queue, mat) + | null mat = n + | null queue = head $ Map.toList mat -- cgit v1.2.3