aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md2
-rw-r--r--aoc-2022-dotnet/Day05/Day05.fsproj22
-rw-r--r--aoc-2022-dotnet/Day05/Program.fs67
-rw-r--r--aoc-2022-dotnet/README.md6
-rw-r--r--aoc-2022-dotnet/aoc-2022-dotnet.sln8
5 files changed, 100 insertions, 5 deletions
diff --git a/README.md b/README.md
index 4903ec4..6df389c 100644
--- a/README.md
+++ b/README.md
@@ -10,7 +10,7 @@ Repository storing my solutions to Advent of Code. See other people's solutions
### [2022](aoc-2022-dotnet)
![.NET](https://img.shields.io/badge/.NET-grey?logo=.NET)
-![Stars](https://img.shields.io/badge/🌟%20stars-8/50-orange)
+![Stars](https://img.shields.io/badge/🌟%20stars-10/50-orange)
[awesome]: https://github.com/Bogdanp/awesome-advent-of-code
[aoc]: https://adventofcode.com
diff --git a/aoc-2022-dotnet/Day05/Day05.fsproj b/aoc-2022-dotnet/Day05/Day05.fsproj
new file mode 100644
index 0000000..44c3fba
--- /dev/null
+++ b/aoc-2022-dotnet/Day05/Day05.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="FParsec" Version="1.1.1" />
+ </ItemGroup>
+
+</Project>
diff --git a/aoc-2022-dotnet/Day05/Program.fs b/aoc-2022-dotnet/Day05/Program.fs
new file mode 100644
index 0000000..e15f53d
--- /dev/null
+++ b/aoc-2022-dotnet/Day05/Program.fs
@@ -0,0 +1,67 @@
+ο»Ώopen System
+open System.IO
+open FParsec
+
+type Move =
+ | Move of int * int * int
+
+ static member parse str =
+ let dec n = n - 1
+ let pPart str = pstring str >>. pint32
+ let pMove = tuple3 (pPart "move ") (pPart " from " |>> dec) (pPart " to " |>> dec)
+
+ match run pMove str with
+ | Success (result, _, _) -> Move result
+ | _ -> failwith "Invalid move format!"
+
+ static member execute order stacks (Move (n, fi, ti)) =
+ List.mapi
+ (fun i x ->
+ if i = fi then
+ List.item fi stacks |> List.skip n
+ elif i = ti then
+ (List.item fi stacks |> List.take n |> order)
+ @ List.item ti stacks
+ else
+ x)
+ stacks
+
+let parseStacks str =
+ let pFullCrate = pchar '[' >>. anyChar .>> pchar ']' |>> Some
+ let pEmptyCrate = pstring " " >>% None
+ let pCrate = pFullCrate <|> pEmptyCrate
+ let pCrateLine = sepBy pCrate (pchar ' ') .>> skipNewline
+ let pHeader = many pCrateLine
+
+ let parsed =
+ match run pHeader str with
+ | Success (result, _, _) -> result
+ | _ -> failwith "Invalid header format!"
+
+ parsed
+ |> List.transpose
+ |> List.map (List.choose id)
+
+let solve order input =
+ let headerList =
+ input
+ |> Seq.takeWhile (not << String.IsNullOrEmpty)
+ |> Seq.toList
+
+ let stacks = parseStacks <| String.Join("\n", headerList)
+
+ String.Concat(
+ input
+ |> Seq.skip (headerList.Length + 1)
+ |> Seq.map Move.parse
+ |> Seq.fold (Move.execute order) stacks
+ |> List.map List.head
+ )
+
+let test = File.ReadLines "test.txt"
+assert (solve List.rev test = "CMZ")
+assert (solve id test = "MCD")
+
+let input = File.ReadLines "input.txt"
+printfn "%s" <| solve List.rev input
+printfn "%s" <| solve id input
diff --git a/aoc-2022-dotnet/README.md b/aoc-2022-dotnet/README.md
index 5bb69a2..b74d6c1 100644
--- a/aoc-2022-dotnet/README.md
+++ b/aoc-2022-dotnet/README.md
@@ -1,6 +1,6 @@
# Advent of Code 2022 in .NET
![.NET](https://img.shields.io/badge/.NET-grey?logo=.NET)
-![Stars](https://img.shields.io/badge/🌟%20stars-8/50-orange)
+![Stars](https://img.shields.io/badge/🌟%20stars-10/50-orange)
## Progress
| Day | Part 1 | Part 2 |
@@ -8,8 +8,8 @@
| Day 1: Calorie Counting | 🌟 | 🌟 |
| Day 2: Rock Paper Scissors | 🌟 | 🌟 |
| Day 3: Rucksack Reorganization | 🌟 | 🌟 |
-| Day 4: ??? | 🌟 | 🌟 |
-| Day 5: ??? | | |
+| Day 4: Camp Cleanup | 🌟 | 🌟 |
+| Day 5: Supply Stacks | 🌟 | 🌟 |
| Day 6: ??? | | |
| Day 7: ??? | | |
| Day 8: ??? | | |
diff --git a/aoc-2022-dotnet/aoc-2022-dotnet.sln b/aoc-2022-dotnet/aoc-2022-dotnet.sln
index b89b48a..06c19f4 100644
--- a/aoc-2022-dotnet/aoc-2022-dotnet.sln
+++ b/aoc-2022-dotnet/aoc-2022-dotnet.sln
@@ -16,7 +16,9 @@ Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "Day02", "Day02\Day02.fsproj
EndProject
Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "Day03", "Day03\Day03.fsproj", "{EB78C4D1-7BDD-42AF-820B-32F4102190B1}"
EndProject
-Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Day04", "Day04\Day04.fsproj", "{3E74A210-46A7-420E-93B4-12CCC4B51957}"
+Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "Day04", "Day04\Day04.fsproj", "{3E74A210-46A7-420E-93B4-12CCC4B51957}"
+EndProject
+Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Day05", "Day05\Day05.fsproj", "{4D538535-1283-4EEF-80F7-72A9A1643615}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -40,6 +42,10 @@ Global
{3E74A210-46A7-420E-93B4-12CCC4B51957}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3E74A210-46A7-420E-93B4-12CCC4B51957}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3E74A210-46A7-420E-93B4-12CCC4B51957}.Release|Any CPU.Build.0 = Release|Any CPU
+ {4D538535-1283-4EEF-80F7-72A9A1643615}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {4D538535-1283-4EEF-80F7-72A9A1643615}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {4D538535-1283-4EEF-80F7-72A9A1643615}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {4D538535-1283-4EEF-80F7-72A9A1643615}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE