aboutsummaryrefslogtreecommitdiff
path: root/Puzzle10.hs
diff options
context:
space:
mode:
authorYuchen Pei <me@ypei.me>2017-12-11 16:38:28 +0100
committerYuchen Pei <me@ypei.me>2017-12-11 16:38:28 +0100
commit5ef88a61e55d7103553fa6b81df5e2a46009c204 (patch)
tree98ebc6277f070a1f6a96e37acbc68a9e2786dae4 /Puzzle10.hs
parent325e3fa7af74449bbdb2048f5e20684ba6019095 (diff)
finished Day 10
Diffstat (limited to 'Puzzle10.hs')
-rw-r--r--Puzzle10.hs21
1 files changed, 21 insertions, 0 deletions
diff --git a/Puzzle10.hs b/Puzzle10.hs
index 4909eac..fc01499 100644
--- a/Puzzle10.hs
+++ b/Puzzle10.hs
@@ -1,4 +1,7 @@
import Data.List.Split (splitOn)
+import Data.Bits (xor)
+import Numeric (showHex)
+import Data.Char (ord)
split3Ways :: [Int] -> Int -> Int -> ([Int], [Int], [Int])
split3Ways xs from to
@@ -28,4 +31,22 @@ parseInput = (fmap read) . (splitOn ",")
solve1 :: [Char] -> Int
solve1 = solve1' . parseInput
+sparseHash :: [Int] -> [Int]
+sparseHash xs = let (ys, _, _) = foldl step ([0 .. 255], 0, 0) $ take (64 * (length xs)) (cycle xs) in ys
+
+stepHash :: (Int, [Int]) -> (Int, [Int])
+stepHash (xs, ys) = (foldl1 xor (take 16 ys), drop 16 ys)
+
+int2Hex :: Int -> [Char]
+int2Hex xs
+ | length ys == 2 = ys
+ | otherwise = '0':ys
+ where ys = showHex xs ""
+
+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]
+
input = "120,93,0,90,5,80,129,74,1,165,204,255,254,2,50,113"