diff options
author | Tomasz Chojnacki <tomaszchojnacki2001@gmail.com> | 2022-12-22 13:43:09 +0100 |
---|---|---|
committer | Tomasz Chojnacki <tomaszchojnacki2001@gmail.com> | 2022-12-22 13:43:09 +0100 |
commit | c8a6d6010bddc3789f4770e66da3766fc4f470ad (patch) | |
tree | 1448ef78036248b46343d7e981e31d96ebc88966 /aoc-2022-dotnet | |
parent | 9e378a9f80260c2f729daaf83d8e74f7bad70b16 (diff) | |
download | gleam_aoc2020-c8a6d6010bddc3789f4770e66da3766fc4f470ad.tar.gz gleam_aoc2020-c8a6d6010bddc3789f4770e66da3766fc4f470ad.zip |
Finish day 18
Diffstat (limited to 'aoc-2022-dotnet')
-rw-r--r-- | aoc-2022-dotnet/Common/Common.fsproj | 1 | ||||
-rw-r--r-- | aoc-2022-dotnet/Common/Vec3.fs | 33 | ||||
-rw-r--r-- | aoc-2022-dotnet/Day18/Day18.fsproj | 22 | ||||
-rw-r--r-- | aoc-2022-dotnet/Day18/Program.fs | 51 | ||||
-rw-r--r-- | aoc-2022-dotnet/README.md | 8 | ||||
-rw-r--r-- | aoc-2022-dotnet/aoc-2022-dotnet.sln | 8 |
6 files changed, 118 insertions, 5 deletions
diff --git a/aoc-2022-dotnet/Common/Common.fsproj b/aoc-2022-dotnet/Common/Common.fsproj index 32057cf..510a1ea 100644 --- a/aoc-2022-dotnet/Common/Common.fsproj +++ b/aoc-2022-dotnet/Common/Common.fsproj @@ -6,6 +6,7 @@ </PropertyGroup> <ItemGroup> + <Compile Include="Vec3.fs" /> <Compile Include="Vec2.fs" /> <Compile Include="Util.fs" /> </ItemGroup> diff --git a/aoc-2022-dotnet/Common/Vec3.fs b/aoc-2022-dotnet/Common/Vec3.fs new file mode 100644 index 0000000..3a05315 --- /dev/null +++ b/aoc-2022-dotnet/Common/Vec3.fs @@ -0,0 +1,33 @@ +namespace Common + +type Vec3 = + | Vec3 of int * int * int + + static member x(Vec3 (x, _, _)) = x + static member y(Vec3 (_, y, _)) = y + static member z(Vec3 (_, _, z)) = z + + static member ones = Vec3(1, 1, 1) + + static member directions6 = + [ Vec3(-1, 0, 0) + Vec3(1, 0, 0) + Vec3(0, -1, 0) + Vec3(0, 1, 0) + Vec3(0, 0, -1) + Vec3(0, 0, 1) ] + + static member combine fn (Vec3 (x1, y1, z1)) (Vec3 (x2, y2, z2)) = Vec3(fn x1 x2, fn y1 y2, fn z1 z2) + + static member inline (+)(v1, v2) = Vec3.combine (+) v1 v2 + static member inline (-)(v1, v2) = Vec3.combine (-) v1 v2 + + static member neighbours6 v = List.map ((+) v) Vec3.directions6 + + static member boundedBy minBound maxBound (Vec3 (x, y, z)) = + x >= Vec3.x minBound + && x <= Vec3.x maxBound + && y >= Vec3.y minBound + && y <= Vec3.y maxBound + && z >= Vec3.z minBound + && z <= Vec3.z maxBound diff --git a/aoc-2022-dotnet/Day18/Day18.fsproj b/aoc-2022-dotnet/Day18/Day18.fsproj new file mode 100644 index 0000000..b9a3919 --- /dev/null +++ b/aoc-2022-dotnet/Day18/Day18.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> + <ProjectReference Include="..\Common\Common.fsproj" /> + </ItemGroup> + +</Project> diff --git a/aoc-2022-dotnet/Day18/Program.fs b/aoc-2022-dotnet/Day18/Program.fs new file mode 100644 index 0000000..3784d0d --- /dev/null +++ b/aoc-2022-dotnet/Day18/Program.fs @@ -0,0 +1,51 @@ +module Day18 + +open System.IO +open FParsec +open Common + +let parsePos = + Util.parse ( + tuple3 (pint32 .>> pchar ',') (pint32 .>> pchar ',') pint32 + |>> Vec3 + ) + +let fullSurface cubes = + cubes + |> Seq.collect Vec3.neighbours6 + |> Seq.filter (Util.notIn cubes) + |> Seq.length + +let externalSurface cubes = + let hasLava pos = Set.contains pos cubes + + let minBound = Seq.reduce (Vec3.combine min) cubes - Vec3.ones + let maxBound = Seq.reduce (Vec3.combine max) cubes + Vec3.ones + + let neighbours pos = + match hasLava pos with + | true -> [] + | false -> List.filter (Vec3.boundedBy minBound maxBound) (Vec3.neighbours6 pos) + + let rec dfs visited count = + function + | pos :: stackTail -> + let count' = count + if hasLava pos then 1 else 0 + + match Set.contains pos visited with + | true -> dfs visited count' stackTail + | false -> dfs (Set.add pos visited) count' (neighbours pos @ stackTail) + | [] -> count + + dfs Set.empty 0 [ minBound ] + +let solution surface = + Seq.map parsePos >> Set.ofSeq >> surface + +let test = File.ReadLines("test.txt") +assert (solution fullSurface test = 64) +assert (solution externalSurface test = 2540) + +let input = File.ReadLines("input.txt") +printfn "%d" <| solution fullSurface input +printfn "%d" <| solution externalSurface input diff --git a/aoc-2022-dotnet/README.md b/aoc-2022-dotnet/README.md index 25192a0..8d6d1a8 100644 --- a/aoc-2022-dotnet/README.md +++ b/aoc-2022-dotnet/README.md @@ -1,6 +1,6 @@ # Advent of Code 2022 in F#  - + | Day | Problem Name | Part 1 | Part 2 | Practiced Concepts | | :----: | ---------------------------------------------------------------- | :----: | :----: | ---------------------------------------------------- | @@ -12,7 +12,7 @@ | **06** | [Tuning Trouble](https://adventofcode.com/2022/day/6) | :star: | :star: | `Seq.windowed`, `Set` | | **07** | [No Space Left On Device](https://adventofcode.com/2022/day/7) | :star: | :star: | patterns, ADTs, FParsec | | **08** | [Treetop Tree House](https://adventofcode.com/2022/day/8) | :star: | :star: | `Array2D`, slices | -| **09** | [Rope Bridge](https://adventofcode.com/2022/day/9) | :star: | :star: | vectors, Chebyshev distance, `Seq.scan` | +| **09** | [Rope Bridge](https://adventofcode.com/2022/day/9) | :star: | :star: | 2D vectors, Chebyshev distance, `Seq.scan` | | **10** | [Cathode-Ray Tube](https://adventofcode.com/2022/day/10) | :star: | :star: | `Map`, `Seq.fold` | | **11** | [Monkey in the Middle](https://adventofcode.com/2022/day/11) | :star: | :star: | records, type abbrevs, `List.choose`, tail recursion | | **12** | [Hill Climbing Algorithm](https://adventofcode.com/2022/day/12) | :star: | :star: | functional graphs, functional BFS, `Array2D` | @@ -21,11 +21,11 @@ | **15** | [Beacon Exclusion Zone](https://adventofcode.com/2022/day/15) | :star: | :star: | ranges, mahattan distance, parallel computing | | **16** | [Proboscidea Volcanium](https://adventofcode.com/2022/day/16) | :star: | :star: | graphs, dynamic programming, `Seq` | | **17** | [Pyroclastic Flow](https://adventofcode.com/2022/day/17) | :star: | :star: | cycle detection, bitwise operators, hashing | -| **18** | [Boiling Boulders](https://adventofcode.com/2022/day/18) | | | | +| **18** | [Boiling Boulders](https://adventofcode.com/2022/day/18) | :star: | :star: | functional DFS, 3D vectors, position bound checking | | **19** | [Not Enough Minerals](https://adventofcode.com/2022/day/19) | | | | | **20** | [Grove Positioning System](https://adventofcode.com/2022/day/20) | | | | | **21** | [Monkey Math](https://adventofcode.com/2022/day/21) | | | | -| **22** | ??? | | | | +| **22** | [Monkey Map](https://adventofcode.com/2022/day/22) | | | | | **23** | ??? | | | | | **24** | ??? | | | | | **25** | ??? | | | | diff --git a/aoc-2022-dotnet/aoc-2022-dotnet.sln b/aoc-2022-dotnet/aoc-2022-dotnet.sln index c8afbfc..32a106b 100644 --- a/aoc-2022-dotnet/aoc-2022-dotnet.sln +++ b/aoc-2022-dotnet/aoc-2022-dotnet.sln @@ -46,7 +46,9 @@ Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "Day15", "Day15\Day15.fsproj EndProject Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "Day16", "Day16\Day16.fsproj", "{4CABEF49-D9D8-4211-8714-95E118820C8F}" EndProject -Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Day17", "Day17\Day17.fsproj", "{7DEA7521-24E1-4ED4-9530-70E91731ADB3}" +Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "Day17", "Day17\Day17.fsproj", "{7DEA7521-24E1-4ED4-9530-70E91731ADB3}" +EndProject +Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Day18", "Day18\Day18.fsproj", "{31BA1C7C-D4C3-4F57-8129-8EF280911878}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -130,6 +132,10 @@ Global {7DEA7521-24E1-4ED4-9530-70E91731ADB3}.Debug|Any CPU.Build.0 = Debug|Any CPU {7DEA7521-24E1-4ED4-9530-70E91731ADB3}.Release|Any CPU.ActiveCfg = Release|Any CPU {7DEA7521-24E1-4ED4-9530-70E91731ADB3}.Release|Any CPU.Build.0 = Release|Any CPU + {31BA1C7C-D4C3-4F57-8129-8EF280911878}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {31BA1C7C-D4C3-4F57-8129-8EF280911878}.Debug|Any CPU.Build.0 = Debug|Any CPU + {31BA1C7C-D4C3-4F57-8129-8EF280911878}.Release|Any CPU.ActiveCfg = Release|Any CPU + {31BA1C7C-D4C3-4F57-8129-8EF280911878}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE |