diff options
author | Tomasz Chojnacki <tomaszchojnacki2001@gmail.com> | 2022-12-02 18:43:39 +0100 |
---|---|---|
committer | Tomasz Chojnacki <tomaszchojnacki2001@gmail.com> | 2022-12-02 18:43:39 +0100 |
commit | 9889c41f556eb134527909b7e0f29224786d33ec (patch) | |
tree | 8513d08b953d0c7eff6a8ef895317d36f4fd2991 /aoc-2022-dotnet/Day02 | |
parent | 0705a262782338deb9639304a798429e8792f5ec (diff) | |
download | gleam_aoc2020-9889c41f556eb134527909b7e0f29224786d33ec.tar.gz gleam_aoc2020-9889c41f556eb134527909b7e0f29224786d33ec.zip |
Upload first two days of year 2022
Diffstat (limited to 'aoc-2022-dotnet/Day02')
-rw-r--r-- | aoc-2022-dotnet/Day02/Day02.fsproj | 22 | ||||
-rw-r--r-- | aoc-2022-dotnet/Day02/Program.fs | 53 |
2 files changed, 75 insertions, 0 deletions
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) |