diff options
author | Tomasz Chojnacki <tomaszchojnacki2001@gmail.com> | 2022-12-02 21:02:32 +0100 |
---|---|---|
committer | Tomasz Chojnacki <tomaszchojnacki2001@gmail.com> | 2022-12-02 21:02:32 +0100 |
commit | 432a4dd0fb6cea783a4c60480f64e4724730d71f (patch) | |
tree | 58d37cb6fa29a60fbc2deb4cdda26df33f2b3c1a /aoc-2022-dotnet | |
parent | 9889c41f556eb134527909b7e0f29224786d33ec (diff) | |
download | gleam_aoc2020-432a4dd0fb6cea783a4c60480f64e4724730d71f.tar.gz gleam_aoc2020-432a4dd0fb6cea783a4c60480f64e4724730d71f.zip |
Refactor day 1 solution
Diffstat (limited to 'aoc-2022-dotnet')
-rw-r--r-- | aoc-2022-dotnet/Day01/Day01.fsproj | 4 | ||||
-rw-r--r-- | aoc-2022-dotnet/Day01/Program.fs | 77 |
2 files changed, 45 insertions, 36 deletions
diff --git a/aoc-2022-dotnet/Day01/Day01.fsproj b/aoc-2022-dotnet/Day01/Day01.fsproj index e337a41..0e98abc 100644 --- a/aoc-2022-dotnet/Day01/Day01.fsproj +++ b/aoc-2022-dotnet/Day01/Day01.fsproj @@ -15,4 +15,8 @@ <Compile Include="Program.fs" /> </ItemGroup> + <ItemGroup> + <PackageReference Include="FSharpPlus" Version="1.3.2" /> + </ItemGroup> + </Project> diff --git a/aoc-2022-dotnet/Day01/Program.fs b/aoc-2022-dotnet/Day01/Program.fs index ecc35f1..560e770 100644 --- a/aoc-2022-dotnet/Day01/Program.fs +++ b/aoc-2022-dotnet/Day01/Program.fs @@ -1,37 +1,42 @@ open System.IO - -let parseLine = function "" -> None | l -> Some(int l) -let readInput path = File.ReadLines path |> Seq.map parseLine - -let splitOnNone xs = - let (lastGroup, result) = Seq.fold (fun (groupAcc, resultAcc) x -> - match x with - | Some(value) -> (value :: groupAcc, resultAcc) - | None -> (List.empty, (List.rev groupAcc) :: resultAcc)) (List.empty, List.empty) xs - in List.rev (if List.isEmpty lastGroup then result else lastGroup :: result) - -let groupCalorySums data = data |> splitOnNone |> List.map List.sum - -let mostTotalCalories data = data |> groupCalorySums |> List.max - -let rec insert x xs = - match xs with - | [] -> [x] - | y::ys -> if x < y then x::y::ys else y::insert x ys - -let topN n data = - Seq.fold (fun acc elem -> - if List.length acc < n then insert elem acc - elif List.head acc < elem then insert elem (List.tail acc) - else acc - ) List.empty data - -let top3TotalCaloriesSum data = data |> groupCalorySums |> topN 3 |> List.sum - -let test = readInput "test.txt" -assert (mostTotalCalories test = 24000) -assert (top3TotalCaloriesSum test = 45000) - -let input = readInput "input.txt" -printfn "%d" (mostTotalCalories input) -printfn "%d" (top3TotalCaloriesSum input) +open FSharpPlus + +let parseLine = + function + | "" -> -1 + | l -> int l + +let caloriesPerElf data = + data |> 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 elem -> + if List.length acc < n then + insertSorted elem acc + elif List.head acc < elem then + insertSorted elem (List.tail acc) + else + acc) + List.empty + xs + +let solution n input = + input + |> 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) |