aboutsummaryrefslogtreecommitdiff
path: root/Puzzle1.hs
blob: dc39b61be811cf3c29e489b487e12e9ac257daaf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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