-
Notifications
You must be signed in to change notification settings - Fork 0
/
1.hs
26 lines (22 loc) · 846 Bytes
/
1.hs
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
{-# LANGUAGE Safe #-}
import Data.Char
import Data.Monoid
import System.IO
neighborDigits :: Int -> [Int] -> [(Int, Int)]
neighborDigits n = take <$> length <*> (zip <$> cycle <*> (drop n . cycle))
matchingDigitsSum :: Int -> [Int] -> Int
matchingDigitsSum n = foldSum
. map fst
. filter (identicalPair)
. neighborDigits n
where foldSum = getSum . foldMap Sum
identicalPair = uncurry (==)
main = do
withFile "1.txt" ReadMode (\hdl -> do
inputDigits <- fmap digits $ hGetLine hdl
let inputMatchingDigitsSum = flip matchingDigitsSum inputDigits
let putSum = putStrLnShow . inputMatchingDigitsSum
putSum 1
putSum $ (length inputDigits) `quot` 2)
where putStrLnShow = putStrLn . show
digits = map digitToInt