aboutsummaryrefslogtreecommitdiff
path: root/Puzzle1.hs
diff options
context:
space:
mode:
Diffstat (limited to 'Puzzle1.hs')
-rw-r--r--Puzzle1.hs25
1 files changed, 25 insertions, 0 deletions
diff --git a/Puzzle1.hs b/Puzzle1.hs
new file mode 100644
index 0000000..dc39b61
--- /dev/null
+++ b/Puzzle1.hs
@@ -0,0 +1,25 @@
+import Data.Char (digitToInt)
+
+solve1 :: [Char] -> Int
+solve1 xs = let ys = digitToInt <$> xs in firstAndLast ys + solve' ys
+
+solve' :: [Int] -> Int
+solve' [] = 0
+solve' [x] = 0
+solve' (x:y:xs) = if x == y then x + solve' (y:xs) else solve' (y:xs)
+
+firstAndLast :: [Int] -> Int
+firstAndLast [] = 0
+firstAndLast [x] = 0
+firstAndLast xs = if head xs == last xs then head xs else 0
+
+splitInput :: [Int] -> ([Int], [Int])
+splitInput xs = splitAt (length xs `div` 2) xs
+
+sumSame :: ([Int], [Int]) -> Int
+sumSame ([], _) = 0
+sumSame (_, []) = 0
+sumSame (x:xs, y:ys) = if x == y then x * 2 + sumSame (xs, ys) else sumSame (xs, ys)
+
+solve2 :: [Char] -> Int
+solve2 xs = sumSame $ splitInput $ digitToInt <$> xs