blob: 71faaae76cb0e60418852c88baad91dd51be4a88 (
plain)
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
32
33
34
35
36
37
38
39
40
41
42
|
module Day01
open System.IO
open FSharpPlus
let parseLine =
function
| "" -> -1
| l -> int l
let caloriesPerElf = Seq.split [ [ -1 ] ] >> Seq.map Seq.sum
let topN n xs =
let rec insertSorted x =
function
| h :: t -> min h x :: (insertSorted (max h x) t)
| _ -> [ x ]
Seq.fold
(fun acc x ->
if List.length acc < n then
insertSorted x acc
elif List.head acc < x then
insertSorted x <| List.tail acc
else
acc)
List.empty
xs
let solution n =
Seq.map parseLine
>> caloriesPerElf
>> topN n
>> List.sum
let test = File.ReadLines "test.txt"
assert (solution 1 test = 24000)
assert (solution 3 test = 45000)
let input = File.ReadLines "input.txt"
printfn "%d" <| solution 1 input
printfn "%d" <| solution 3 input
|