aboutsummaryrefslogtreecommitdiff
path: root/aoc-2022-dotnet
diff options
context:
space:
mode:
authorTomasz Chojnacki <tomaszchojnacki2001@gmail.com>2022-12-02 18:43:39 +0100
committerTomasz Chojnacki <tomaszchojnacki2001@gmail.com>2022-12-02 18:43:39 +0100
commit9889c41f556eb134527909b7e0f29224786d33ec (patch)
tree8513d08b953d0c7eff6a8ef895317d36f4fd2991 /aoc-2022-dotnet
parent0705a262782338deb9639304a798429e8792f5ec (diff)
downloadgleam_aoc2020-9889c41f556eb134527909b7e0f29224786d33ec.tar.gz
gleam_aoc2020-9889c41f556eb134527909b7e0f29224786d33ec.zip
Upload first two days of year 2022
Diffstat (limited to 'aoc-2022-dotnet')
-rw-r--r--aoc-2022-dotnet/Day01/Day01.fsproj18
-rw-r--r--aoc-2022-dotnet/Day01/Program.fs37
-rw-r--r--aoc-2022-dotnet/Day02/Day02.fsproj22
-rw-r--r--aoc-2022-dotnet/Day02/Program.fs53
-rw-r--r--aoc-2022-dotnet/aoc-2022-dotnet.sln36
5 files changed, 166 insertions, 0 deletions
diff --git a/aoc-2022-dotnet/Day01/Day01.fsproj b/aoc-2022-dotnet/Day01/Day01.fsproj
new file mode 100644
index 0000000..e337a41
--- /dev/null
+++ b/aoc-2022-dotnet/Day01/Day01.fsproj
@@ -0,0 +1,18 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+ <PropertyGroup>
+ <OutputType>Exe</OutputType>
+ <TargetFramework>net7.0</TargetFramework>
+ </PropertyGroup>
+
+ <ItemGroup>
+ <Content Include="test.txt">
+ <CopyToOutputDirectory>Always</CopyToOutputDirectory>
+ </Content>
+ <Content Include="input.txt">
+ <CopyToOutputDirectory>Always</CopyToOutputDirectory>
+ </Content>
+ <Compile Include="Program.fs" />
+ </ItemGroup>
+
+</Project>
diff --git a/aoc-2022-dotnet/Day01/Program.fs b/aoc-2022-dotnet/Day01/Program.fs
new file mode 100644
index 0000000..ecc35f1
--- /dev/null
+++ b/aoc-2022-dotnet/Day01/Program.fs
@@ -0,0 +1,37 @@
+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)
diff --git a/aoc-2022-dotnet/Day02/Day02.fsproj b/aoc-2022-dotnet/Day02/Day02.fsproj
new file mode 100644
index 0000000..0e98abc
--- /dev/null
+++ b/aoc-2022-dotnet/Day02/Day02.fsproj
@@ -0,0 +1,22 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+ <PropertyGroup>
+ <OutputType>Exe</OutputType>
+ <TargetFramework>net7.0</TargetFramework>
+ </PropertyGroup>
+
+ <ItemGroup>
+ <Content Include="test.txt">
+ <CopyToOutputDirectory>Always</CopyToOutputDirectory>
+ </Content>
+ <Content Include="input.txt">
+ <CopyToOutputDirectory>Always</CopyToOutputDirectory>
+ </Content>
+ <Compile Include="Program.fs" />
+ </ItemGroup>
+
+ <ItemGroup>
+ <PackageReference Include="FSharpPlus" Version="1.3.2" />
+ </ItemGroup>
+
+</Project>
diff --git a/aoc-2022-dotnet/Day02/Program.fs b/aoc-2022-dotnet/Day02/Program.fs
new file mode 100644
index 0000000..4f045dd
--- /dev/null
+++ b/aoc-2022-dotnet/Day02/Program.fs
@@ -0,0 +1,53 @@
+open System.IO
+open FSharpPlus
+
+type Move = R | P | S
+type Result = Win | Draw | Lose
+exception ParseError
+
+let parseEnemyMove = function "A" -> R | "B" -> P | "C" -> S | _ -> raise ParseError
+let parseAllyMove = function "X" -> R | "Y" -> P | "Z" -> S | _ -> raise ParseError
+let parseOutcome = function "X" -> Lose | "Y" -> Draw | "Z" -> Win | _ -> raise ParseError
+
+let roundScore round =
+ let selectionScore = function R -> 1 | P -> 2 | S -> 3
+
+ let outcomeScore = function
+ | (R, S) | (S, P) | (P, R) -> 0
+ | (R, R) | (P, P) | (S, S) -> 3
+ | (R, P) | (P, S) | (S, R) -> 6
+
+ selectionScore (snd round) + outcomeScore round
+
+let lineToTuple line =
+ match String.split [" "] line |> Seq.toList with
+ | [first; second] -> first, second
+ | _ -> raise ParseError
+
+let selectMove outcome enemyMove =
+ match (outcome, enemyMove) with
+ | (Win, R) -> P | (Draw, R) -> R | (Lose, R) -> S
+ | (Win, P) -> S | (Draw, P) -> P | (Lose, P) -> R
+ | (Win, S) -> R | (Draw, S) -> S | (Lose, S) -> P
+
+let parseRoundV1 roundStr =
+ let (firstStr, secondStr) = lineToTuple roundStr
+ parseEnemyMove firstStr, parseAllyMove secondStr
+
+let parseRoundV2 roundStr =
+ let (firstStr, secondStr) = lineToTuple roundStr
+ let enemyMove = parseEnemyMove firstStr
+ let outcome = parseOutcome secondStr
+ let allyMove = selectMove outcome enemyMove
+ enemyMove, allyMove
+
+let rateStrategyGuideV1 input = input |> Seq.map parseRoundV1 |> Seq.sumBy roundScore
+let rateStrategyGuideV2 input = input |> Seq.map parseRoundV2 |> Seq.sumBy roundScore
+
+let test = File.ReadLines "test.txt"
+assert (rateStrategyGuideV1 test = 15)
+assert (rateStrategyGuideV2 test = 12)
+
+let input = File.ReadLines "input.txt"
+printfn "%d" (rateStrategyGuideV1 input)
+printfn "%d" (rateStrategyGuideV2 input)
diff --git a/aoc-2022-dotnet/aoc-2022-dotnet.sln b/aoc-2022-dotnet/aoc-2022-dotnet.sln
new file mode 100644
index 0000000..87e308c
--- /dev/null
+++ b/aoc-2022-dotnet/aoc-2022-dotnet.sln
@@ -0,0 +1,36 @@
+
+Microsoft Visual Studio Solution File, Format Version 12.00
+# Visual Studio Version 17
+VisualStudioVersion = 17.4.33110.190
+MinimumVisualStudioVersion = 10.0.40219.1
+Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "Day01", "Day01\Day01.fsproj", "{117C087A-8139-4096-822D-2A0BBC2E48FC}"
+EndProject
+Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{45FD9816-5DE7-4422-A8D5-C017F4E5BD2C}"
+ ProjectSection(SolutionItems) = preProject
+ ..\.gitignore = ..\.gitignore
+ EndProjectSection
+EndProject
+Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Day02", "Day02\Day02.fsproj", "{A5148468-D518-4678-B32C-D3C59B6290AB}"
+EndProject
+Global
+ GlobalSection(SolutionConfigurationPlatforms) = preSolution
+ Debug|Any CPU = Debug|Any CPU
+ Release|Any CPU = Release|Any CPU
+ EndGlobalSection
+ GlobalSection(ProjectConfigurationPlatforms) = postSolution
+ {117C087A-8139-4096-822D-2A0BBC2E48FC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {117C087A-8139-4096-822D-2A0BBC2E48FC}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {117C087A-8139-4096-822D-2A0BBC2E48FC}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {117C087A-8139-4096-822D-2A0BBC2E48FC}.Release|Any CPU.Build.0 = Release|Any CPU
+ {A5148468-D518-4678-B32C-D3C59B6290AB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {A5148468-D518-4678-B32C-D3C59B6290AB}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {A5148468-D518-4678-B32C-D3C59B6290AB}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {A5148468-D518-4678-B32C-D3C59B6290AB}.Release|Any CPU.Build.0 = Release|Any CPU
+ EndGlobalSection
+ GlobalSection(SolutionProperties) = preSolution
+ HideSolutionNode = FALSE
+ EndGlobalSection
+ GlobalSection(ExtensibilityGlobals) = postSolution
+ SolutionGuid = {F58FF8AE-DFEE-4153-9686-49D6E097BC2C}
+ EndGlobalSection
+EndGlobal