aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYuchen Pei <me@ypei.me>2017-12-13 12:12:12 +0100
committerYuchen Pei <me@ypei.me>2017-12-13 12:12:12 +0100
commit559c82014b8be5f3837f86b1ccc719baaf5b391b (patch)
tree53a49962274461cc82156af1f9c3889c2064bb8e
parent0eac7352c1f7027e197734ce86b386497176ea06 (diff)
finished day 13
-rw-r--r--Puzzle13.hs25
1 files changed, 25 insertions, 0 deletions
diff --git a/Puzzle13.hs b/Puzzle13.hs
new file mode 100644
index 0000000..7423298
--- /dev/null
+++ b/Puzzle13.hs
@@ -0,0 +1,25 @@
+import Data.List.Split (splitOn)
+
+f :: (Int, Int) -> Int
+f (x, y) = if (y == 1) || (x `mod` (y * 2 - 2) == 0) then x * y else 0
+
+g :: Int -> (Int, Int) -> Bool
+g z (x, y) = (z + x) `mod` (2 * y - 2) /= 0
+
+solve2' :: Int -> [(Int, Int)] -> Int
+solve2' z xs
+ | all (g z) xs = z
+ | otherwise = solve2' (z + 1) xs
+
+solve2 :: [Char] -> Int
+solve2 = (solve2' 0) . parseInput
+
+parseInput :: [Char] -> [(Int, Int)]
+parseInput xs = (\ys -> let [x, y] = words ys in (read $ init x, read y)) <$> (splitOn "\n" xs)
+
+solve1 :: [Char] -> Int
+solve1 = sum . (fmap f) . parseInput
+
+test = "0: 3\n1: 2\n4: 4\n6: 4"
+
+input = "0: 3\n1: 2\n2: 6\n4: 4\n6: 4\n8: 8\n10: 6\n12: 8\n14: 5\n16: 6\n18: 8\n20: 8\n22: 12\n24: 6\n26: 9\n28: 8\n30: 12\n32: 12\n34: 17\n36: 12\n38: 8\n40: 12\n42: 12\n44: 10\n46: 12\n48: 12\n50: 12\n52: 14\n54: 14\n56: 10\n58: 14\n60: 12\n62: 14\n64: 14\n66: 14\n68: 14\n70: 14\n72: 14\n74: 14\n76: 14\n86: 14\n94: 20\n96: 18"