-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay1.hs
More file actions
31 lines (26 loc) · 748 Bytes
/
Day1.hs
File metadata and controls
31 lines (26 loc) · 748 Bytes
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
module Day1
( part1
, part2
) where
import Data.Char (digitToInt, isDigit)
import Data.List as L (length)
import Data.Vector as V (Vector, fromList, length, (!))
count :: Int -> Vector Int -> Int
count step captcha = foldr compute 0 [0 .. V.length captcha - 1]
where
compute v acc =
comp (captcha ! v) (captcha ! ((v + step) `mod` V.length captcha)) + acc
comp :: Int -> Int -> Int
comp a b
| a == b = a
| otherwise = 0
part1 :: Bool -> String -> String
part1 _ = show . count 1 . fromList . map digitToInt . filter isDigit
part2 :: Bool -> String -> String
part2 _ input =
show
. count (L.length input `div` 2)
. fromList
. map digitToInt
. filter isDigit
$ input