diff options
author | Tomasz Chojnacki <tomaszchojnacki2001@gmail.com> | 2022-12-03 12:08:40 +0100 |
---|---|---|
committer | Tomasz Chojnacki <tomaszchojnacki2001@gmail.com> | 2022-12-03 12:08:40 +0100 |
commit | df1680c97bf2dbfc844ad0cd903665a3f4d8de9f (patch) | |
tree | c2cea6fada774cd4e30f76996eac081b6fdfdf43 /aoc-2022-dotnet | |
parent | 9525f09701d12aa1d93b9ac73878f014abf199a3 (diff) | |
download | gleam_aoc2020-df1680c97bf2dbfc844ad0cd903665a3f4d8de9f.tar.gz gleam_aoc2020-df1680c97bf2dbfc844ad0cd903665a3f4d8de9f.zip |
Refactor first two days
Diffstat (limited to 'aoc-2022-dotnet')
-rw-r--r-- | aoc-2022-dotnet/Day01/Program.fs | 22 | ||||
-rw-r--r-- | aoc-2022-dotnet/Day02/Program.fs | 19 |
2 files changed, 19 insertions, 22 deletions
diff --git a/aoc-2022-dotnet/Day01/Program.fs b/aoc-2022-dotnet/Day01/Program.fs index adcb20e..bf42670 100644 --- a/aoc-2022-dotnet/Day01/Program.fs +++ b/aoc-2022-dotnet/Day01/Program.fs @@ -6,13 +6,12 @@ let parseLine = | "" -> -1 | l -> int l -let caloriesPerElf data = - data |> Seq.split [ [ -1 ] ] |> Seq.map Seq.sum +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) + | h :: t -> min h x :: (insertSorted (max h x) t) | _ -> [ x ] Seq.fold @@ -20,23 +19,22 @@ let topN n xs = if List.length acc < n then insertSorted x acc elif List.head acc < x then - insertSorted x (List.tail acc) + insertSorted x <| List.tail acc else acc) List.empty xs -let solution n input = - input - |> Seq.map parseLine - |> caloriesPerElf - |> topN n - |> List.sum +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) +printfn "%d" <| solution 1 input +printfn "%d" <| solution 3 input diff --git a/aoc-2022-dotnet/Day02/Program.fs b/aoc-2022-dotnet/Day02/Program.fs index b143390..7328ea8 100644 --- a/aoc-2022-dotnet/Day02/Program.fs +++ b/aoc-2022-dotnet/Day02/Program.fs @@ -11,7 +11,7 @@ type Move = | "A" -> Rock | "B" -> Paper | "C" -> Scissors - | _ -> failwith "Invalid move string!" + | s -> failwithf "Invalid move: %s" s static member choices = [ Rock; Paper; Scissors ] @@ -32,10 +32,10 @@ type Strategy = | "X" -> X | "Y" -> Y | "Z" -> Z - | _ -> failwith "Invalid strategy string!" + | s -> failwithf "Invalid strategy: %s" s let splitToTuple sep str = - match String.split [ sep ] str |> Seq.toList with + match Seq.toList <| String.split [ sep ] str with | [ x; y ] -> x, y | _ -> failwith "Invalid string format!" @@ -67,21 +67,20 @@ let guide2 (enemy: Move) = let parseRound guide roundStr = let (enemy, strategy) = - splitToTuple " " roundStr + roundStr + |> splitToTuple " " |> mapItem1 Move.parse |> mapItem2 Strategy.parse enemy, guide enemy strategy -let solution guide input = - input - |> Seq.map (parseRound guide) - |> Seq.sumBy scoreRound +let solution guide = + Seq.map (parseRound guide) >> Seq.sumBy scoreRound let test = File.ReadLines "test.txt" assert (solution guide1 test = 15) assert (solution guide2 test = 12) let input = File.ReadLines "input.txt" -printfn "%d" (solution guide1 input) -printfn "%d" (solution guide2 input) +printfn "%d" <| solution guide1 input +printfn "%d" <| solution guide2 input |