Skip to content

Commit 149cf9a

Browse files
committed
1fsu
1 parent f5e4440 commit 149cf9a

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

f#/1.fsx

+8-14
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,16 @@
33
open Common
44
open System
55

6+
// take a predicate and a list
7+
// if list is empty just return
8+
// if list is not empty then take while the predicate is true; and concatenate with the drop while the predicate is true
9+
610
let p1 (input: List<string>) =
711
input
8-
|> Seq.map (fun s ->
9-
if ((String.IsNullOrWhiteSpace >> not) s) then
10-
Some(int64 s)
11-
else
12-
None)
13-
|> Seq.fold
14-
(fun acc cal ->
15-
let (c, s) = acc
16-
17-
match cal with
18-
| Some v -> (c + v, max (c + v) s)
19-
| None -> (0L, s))
20-
(0, 0)
21-
|> snd
12+
|> splitBy (fun s -> String.IsNullOrWhiteSpace s)
13+
|> List.map (fun l -> List.map int64 l)
14+
|> List.map (fun l -> List.sum l)
15+
|> List.max
2216

2317
let input = Common.readIn
2418
input |> p1 |> Common.writeOut

f#/Common.fsx

+9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@ module Common
22

33
open System
44

5+
let rec splitBy (pred: 'a -> bool) (list: 'a list) =
6+
let index = list |> List.tryFindIndex pred
7+
8+
match index with
9+
| Some i ->
10+
let (frnt, back) = List.splitAt i list
11+
frnt :: (splitBy pred back.Tail)
12+
| None -> [ list ]
13+
514
let readIn =
615
Seq.initInfinite (fun _ -> Console.ReadLine())
716
|> Seq.takeWhile (fun line -> (not (isNull line)))

0 commit comments

Comments
 (0)