-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path13b.hs
27 lines (22 loc) · 772 Bytes
/
13b.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
27
{-# LANGUAGE OverloadedStrings #-}
import qualified Data.Bifunctor as BF
import Data.List
import Data.List.Split
import Data.Tuple
import Prelude
main = do
input <- getContents
putStr $ show $ fn $ splitAndIndexed $ lines input
indexedI :: [a] -> [(Integer, a)]
indexedI = go 0
where
go i (a : as) = (i, a) : go (i + 1) as
go _ _ = []
splitAndIndexed :: [String] -> [(Integer, Integer)]
splitAndIndexed [_, y] = map (BF.second read) $ filter ((/= "x") . snd) $ indexedI $ splitOn "," y
fn :: [(Integer, Integer)] -> Integer
fn = fst . foldl nextPair (0, 1)
where
nextPair (lastMatch, nextSearch) (busIdx, busNo) = (nextMatch, nextSearch * busNo)
where
nextMatch = until (\n -> (n + busIdx) `mod` busNo == 0) (+ nextSearch) lastMatch