{- Copyright (C) 2017 Yuchen Pei. This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . -} 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