From 77e5ab80183dbce5411e2e4f79409494c5e7667a Mon Sep 17 00:00:00 2001 From: Stevan Freeborn <65925598+StevanFreeborn@users.noreply.github.com> Date: Mon, 11 Dec 2023 02:03:46 -0600 Subject: [PATCH] feat: solve part 2 --- 08/HauntedWasteland.Tests/GlobalUsings.cs | 2 +- 09/MirageMaintenance.Tests/GlobalUsings.cs | 2 +- 10/PipeMaze.Tests/GlobalUsings.cs | 3 +- 10/PipeMaze.Tests/MazeTests.cs | 79 +++++++++++++++++++++- 10/PipeMaze.Tests/PipeMaze.Tests.csproj | 6 ++ 10/PipeMaze/Program.cs | 53 ++++++++++++++- 6 files changed, 138 insertions(+), 7 deletions(-) diff --git a/08/HauntedWasteland.Tests/GlobalUsings.cs b/08/HauntedWasteland.Tests/GlobalUsings.cs index 2d5b473..d6f0167 100644 --- a/08/HauntedWasteland.Tests/GlobalUsings.cs +++ b/08/HauntedWasteland.Tests/GlobalUsings.cs @@ -1,3 +1,3 @@ global using FluentAssertions; -global using Xunit; +global using Xunit; \ No newline at end of file diff --git a/09/MirageMaintenance.Tests/GlobalUsings.cs b/09/MirageMaintenance.Tests/GlobalUsings.cs index 2d5b473..d6f0167 100644 --- a/09/MirageMaintenance.Tests/GlobalUsings.cs +++ b/09/MirageMaintenance.Tests/GlobalUsings.cs @@ -1,3 +1,3 @@ global using FluentAssertions; -global using Xunit; +global using Xunit; \ No newline at end of file diff --git a/10/PipeMaze.Tests/GlobalUsings.cs b/10/PipeMaze.Tests/GlobalUsings.cs index 7fef4b0..2d5b473 100644 --- a/10/PipeMaze.Tests/GlobalUsings.cs +++ b/10/PipeMaze.Tests/GlobalUsings.cs @@ -1,2 +1,3 @@ +global using FluentAssertions; + global using Xunit; -global using FluentAssertions; \ No newline at end of file diff --git a/10/PipeMaze.Tests/MazeTests.cs b/10/PipeMaze.Tests/MazeTests.cs index 0172ba7..30e6f0c 100644 --- a/10/PipeMaze.Tests/MazeTests.cs +++ b/10/PipeMaze.Tests/MazeTests.cs @@ -36,13 +36,25 @@ public class MazeTests [MemberData(nameof(TestData.FurthestStepFromStartTestData), MemberType = typeof(TestData))] public void FurthestStepFromStart_WhenGivenInput_ItShouldReturnExpectedFurthestStepFromStart(Maze maze, int expectedFurthestStepFromStart) { - maze.FarthestStepFromStart + maze.GetFarthestStepFromStart() .Should() .Be(expectedFurthestStepFromStart); } + [Theory] + [MemberData(nameof(TestData.AreaTestData), MemberType = typeof(TestData))] + public void Area_WhenGivenInput_ItShouldReturnExpectedArea(string[] input, int expectedArea) + { + var maze = Maze.Parse(input); + maze.GetAreaOfLoop() + .Should() + .Be(expectedArea); + } + public static class TestData { + private static readonly string[] Input = File.ReadAllLines("INPUT.txt"); + private static readonly string[] TestMazeInput = { ".....", @@ -68,6 +80,66 @@ public class MazeTests ] ); + public static IEnumerable AreaTestData => + new List + { + new object[] + { + new string[] + { + "...........", + ".S-------7.", + ".|F-----7|.", + ".||.....||.", + ".||.....||.", + ".|L-7.F-J|.", + ".|..|.|..|.", + ".L--J.L--J.", + "...........", + }, + 4, + }, + new object[] + { + new string[] + { + ".F----7F7F7F7F-7....", + ".|F--7||||||||FJ....", + ".||.FJ||||||||L7....", + "FJL7L7LJLJ||LJ.L-7..", + "L--J.L7...LJS7F-7L7.", + "....F-J..F7FJ|L7L7L7", + "....L7.F7||L7|.L7L7|", + ".....|FJLJ|FJ|F7|.LJ", + "....FJL-7.||.||||...", + "....L---J.LJ.LJLJ...", + }, + 8 + }, + new object[] + { + new string[] + { + "FF7FSF7F7F7F7F7F---7", + "L|LJ||||||||||||F--J", + "FL-7LJLJ||||||LJL-77", + "F--JF--7||LJLJ7F7FJ-", + "L---JF-JLJ.||-FJLJJ7", + "|F|F-JF---7F7-L7L|7|", + "|FFJF7L7F-JF7|JL---7", + "7-L-JL7||F7|L7F-7F7|", + "L.L7LFJ|||||FJL7||LJ", + "L7JLJL-JLJLJL--JLJ.L", + }, + 10 + }, + new object[] + { + Input, + 287, + } + }; + public static IEnumerable FurthestStepFromStartTestData => new List { @@ -112,6 +184,11 @@ public class MazeTests ] ), 8, + }, + new object[] + { + Maze.Parse(Input), + 6870, } }; diff --git a/10/PipeMaze.Tests/PipeMaze.Tests.csproj b/10/PipeMaze.Tests/PipeMaze.Tests.csproj index ee3cd62..29d03a6 100644 --- a/10/PipeMaze.Tests/PipeMaze.Tests.csproj +++ b/10/PipeMaze.Tests/PipeMaze.Tests.csproj @@ -27,4 +27,10 @@ + + + PreserveNewest + + + diff --git a/10/PipeMaze/Program.cs b/10/PipeMaze/Program.cs index ef145e1..0963e43 100644 --- a/10/PipeMaze/Program.cs +++ b/10/PipeMaze/Program.cs @@ -1,4 +1,5 @@ using System.Diagnostics; +using System.Runtime.CompilerServices; namespace PipeMaze; @@ -18,21 +19,31 @@ public class Program return -2; } + var isPart2 = args.Length > 1 && args[1] == "part2"; var input = await File.ReadAllLinesAsync(args[0]); var stopwatch = new Stopwatch(); stopwatch.Start(); - var result = Maze.Parse(input).FarthestStepFromStart; + var maze = Maze.Parse(input); + var result = isPart2 ? maze.GetAreaOfLoop() : maze.GetFarthestStepFromStart(); stopwatch.Stop(); - Console.WriteLine($"The farthest step from the starting pipe is {result}. ({stopwatch.ElapsedMilliseconds}ms)"); + if (isPart2) + { + Console.WriteLine($"The area of the loop is {result}. ({stopwatch.ElapsedMilliseconds}ms)"); + } + else + { + Console.WriteLine($"The farthest step from the start is {result}. ({stopwatch.ElapsedMilliseconds}ms)"); + } return result; } } + public class Maze( int width, int height, @@ -56,7 +67,43 @@ public class Maze( public Pipe? StartingPipe => Pipes.FirstOrDefault(p => p.Type is PipeType.Unknown); - public int FarthestStepFromStart => GetLoop().Count / 2; + public int GetFarthestStepFromStart() => GetLoop().Count / 2; + + public int GetAreaOfLoop() + { + var count = 0; + var loop = GetLoop(); + var areInsideLoop = false; + var verticalBoundaries = new List() + { + PipeType.UpAndDown, + PipeType.UpAndRight, + PipeType.UpAndLeft, + }; + + for (var row = 0; row < Height; row++) + { + for (var column = 0; column < Width; column++) + { + var pipe = loop.FirstOrDefault(p => p.Coordinate.Column == column && p.Coordinate.Row == row); + + if (pipe is null && areInsideLoop is true) + { + count++; + } + + if ( + pipe is not null && + verticalBoundaries.Contains(pipe.Type) is true + ) + { + areInsideLoop = !areInsideLoop; + } + } + } + + return count; + } public List GetLoop() {