aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md2
-rw-r--r--aoc-2022-dotnet/Day07/Day07.fsproj22
-rw-r--r--aoc-2022-dotnet/Day07/Program.fs78
-rw-r--r--aoc-2022-dotnet/README.md4
-rw-r--r--aoc-2022-dotnet/aoc-2022-dotnet.sln8
5 files changed, 110 insertions, 4 deletions
diff --git a/README.md b/README.md
index 8b9295c..a3ed286 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-12/50-orange)
+![Stars](https://img.shields.io/badge/🌟%20stars-14/50-orange)
[awesome]: https://github.com/Bogdanp/awesome-advent-of-code
[aoc]: https://adventofcode.com
diff --git a/aoc-2022-dotnet/Day07/Day07.fsproj b/aoc-2022-dotnet/Day07/Day07.fsproj
new file mode 100644
index 0000000..44c3fba
--- /dev/null
+++ b/aoc-2022-dotnet/Day07/Day07.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/Day07/Program.fs b/aoc-2022-dotnet/Day07/Program.fs
new file mode 100644
index 0000000..89cd728
--- /dev/null
+++ b/aoc-2022-dotnet/Day07/Program.fs
@@ -0,0 +1,78 @@
+ο»Ώopen System.IO
+open FParsec
+
+let fileSizeThreshold = 100_000
+let totalDiskSpace = 70_000_000
+let requiredUnusedSpace = 30_000_000
+
+type Node =
+ | File of int // file size
+ | Dir of string // dir name
+
+type Command =
+ | CD of string // dest
+ | LS of Node list // nodes in current dir
+
+let parseCommands input =
+ let pcd = pstring "$ cd " >>. restOfLine true |>> CD
+ let pfile = pint32 .>> pchar ' ' .>> restOfLine true |>> File
+ let pdir = pstring "dir " >>. restOfLine true |>> Dir
+ let pnode = pfile <|> pdir
+
+ let pls =
+ pstring "$ ls" >>. skipNewline >>. many pnode
+ |>> LS
+
+ let pcmd = pcd <|> pls
+ let pinput = many pcmd
+
+ match run pinput input with
+ | Success (result, _, _) -> result
+ | _ -> failwith "Invalid input format!"
+
+let combine =
+ function
+ | (_, "/") -> []
+ | (_ :: t, "..") -> t
+ | ([], "..") -> failwith "Can't go above root directory!"
+ | (path, dest) -> dest :: path
+
+let buildFilesystem commands =
+ let rec helper path =
+ function
+ | (CD dir) :: t -> helper (combine (path, dir)) t
+ | (LS list) :: t -> (path, list) :: helper path t
+ | [] -> []
+
+ commands |> helper [] |> Map.ofList
+
+let rec directorySize fileSystem path =
+ fileSystem
+ |> Map.find path
+ |> List.sumBy (function
+ | Dir dir -> directorySize fileSystem <| combine (path, dir)
+ | File size -> size)
+
+let part1 = Seq.filter ((>=) fileSizeThreshold) >> Seq.sum
+
+let part2 sizes =
+ let occupiedSpace = Seq.max sizes // root directory size
+ let unusedSpace = totalDiskSpace - occupiedSpace
+ let missingSpace = requiredUnusedSpace - unusedSpace
+ sizes |> Seq.filter ((<=) missingSpace) |> Seq.min
+
+let solution reduceSizes input =
+ let fileSystem = input |> parseCommands |> buildFilesystem
+
+ fileSystem
+ |> Map.keys
+ |> Seq.map (directorySize fileSystem)
+ |> reduceSizes
+
+let test = File.ReadAllText("test.txt")
+assert (solution part1 test = 95437)
+assert (solution part2 test = 24933642)
+
+let input = File.ReadAllText("input.txt")
+printfn "%d" <| solution part1 input
+printfn "%d" <| solution part2 input
diff --git a/aoc-2022-dotnet/README.md b/aoc-2022-dotnet/README.md
index ef93031..7facc6a 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-12/50-orange)
+![Stars](https://img.shields.io/badge/🌟%20stars-14/50-orange)
## Progress
| Day | Part 1 | Part 2 |
@@ -11,7 +11,7 @@
| Day 4: Camp Cleanup | 🌟 | 🌟 |
| Day 5: Supply Stacks | 🌟 | 🌟 |
| Day 6: Tuning Trouble | 🌟 | 🌟 |
-| Day 7: ??? | | |
+| Day 7: ??? | 🌟 | 🌟 |
| Day 8: ??? | | |
| Day 9: ??? | | |
| Day 10: ??? | | |
diff --git a/aoc-2022-dotnet/aoc-2022-dotnet.sln b/aoc-2022-dotnet/aoc-2022-dotnet.sln
index 3cfbda2..88eab36 100644
--- a/aoc-2022-dotnet/aoc-2022-dotnet.sln
+++ b/aoc-2022-dotnet/aoc-2022-dotnet.sln
@@ -20,7 +20,9 @@ Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "Day04", "Day04\Day04.fsproj
EndProject
Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "Day05", "Day05\Day05.fsproj", "{4D538535-1283-4EEF-80F7-72A9A1643615}"
EndProject
-Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Day06", "Day06\Day06.fsproj", "{24E41736-93F0-4222-810F-47B9A740E599}"
+Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "Day06", "Day06\Day06.fsproj", "{24E41736-93F0-4222-810F-47B9A740E599}"
+EndProject
+Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Day07", "Day07\Day07.fsproj", "{5816A6D4-F70A-419E-AB40-C41C0AB5364F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -52,6 +54,10 @@ Global
{24E41736-93F0-4222-810F-47B9A740E599}.Debug|Any CPU.Build.0 = Debug|Any CPU
{24E41736-93F0-4222-810F-47B9A740E599}.Release|Any CPU.ActiveCfg = Release|Any CPU
{24E41736-93F0-4222-810F-47B9A740E599}.Release|Any CPU.Build.0 = Release|Any CPU
+ {5816A6D4-F70A-419E-AB40-C41C0AB5364F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {5816A6D4-F70A-419E-AB40-C41C0AB5364F}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {5816A6D4-F70A-419E-AB40-C41C0AB5364F}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {5816A6D4-F70A-419E-AB40-C41C0AB5364F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE