blob: 63c3f0f019012f9597f5e7583fc08d87542c7735 (
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
{-
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
<https://www.gnu.org/licenses/>.
-}
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
|