File tree 2 files changed +17
-14
lines changed
2 files changed +17
-14
lines changed Original file line number Diff line number Diff line change 3
3
open Common
4
4
open System
5
5
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
+
6
10
let p1 ( input : List < string >) =
7
11
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 -> ( 0 L, 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
22
16
23
17
let input = Common.readIn
24
18
input |> p1 |> Common.writeOut
Original file line number Diff line number Diff line change @@ -2,6 +2,15 @@ module Common
2
2
3
3
open System
4
4
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
+
5
14
let readIn =
6
15
Seq.initInfinite ( fun _ -> Console.ReadLine())
7
16
|> Seq.takeWhile ( fun line -> ( not ( isNull line)))
You can’t perform that action at this time.
0 commit comments