diff options
author | Tomasz Chojnacki <tomaszchojnacki2001@gmail.com> | 2022-12-07 14:00:58 +0100 |
---|---|---|
committer | Tomasz Chojnacki <tomaszchojnacki2001@gmail.com> | 2022-12-07 14:00:58 +0100 |
commit | bea11616e6b3c26b14534feb87317619009236d1 (patch) | |
tree | 4411a03c55e46fa2ab3b8de8b145c676862a809a /aoc-2022-dotnet/Day07 | |
parent | d4577c74e8fbb1143050cb2880f08307eb953945 (diff) | |
download | gleam_aoc2020-bea11616e6b3c26b14534feb87317619009236d1.tar.gz gleam_aoc2020-bea11616e6b3c26b14534feb87317619009236d1.zip |
Finish day 7
Diffstat (limited to 'aoc-2022-dotnet/Day07')
-rw-r--r-- | aoc-2022-dotnet/Day07/Day07.fsproj | 22 | ||||
-rw-r--r-- | aoc-2022-dotnet/Day07/Program.fs | 78 |
2 files changed, 100 insertions, 0 deletions
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 |