feat: solve part 2
This commit is contained in:
@@ -1,3 +1,3 @@
|
||||
global using FluentAssertions;
|
||||
|
||||
global using Xunit;
|
||||
global using Xunit;
|
||||
@@ -1,3 +1,3 @@
|
||||
global using FluentAssertions;
|
||||
|
||||
global using Xunit;
|
||||
global using Xunit;
|
||||
@@ -1,2 +1,3 @@
|
||||
global using FluentAssertions;
|
||||
|
||||
global using Xunit;
|
||||
global using FluentAssertions;
|
||||
@@ -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<object[]> AreaTestData =>
|
||||
new List<object[]>
|
||||
{
|
||||
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<object[]> FurthestStepFromStartTestData =>
|
||||
new List<object[]>
|
||||
{
|
||||
@@ -112,6 +184,11 @@ public class MazeTests
|
||||
]
|
||||
),
|
||||
8,
|
||||
},
|
||||
new object[]
|
||||
{
|
||||
Maze.Parse(Input),
|
||||
6870,
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -27,4 +27,10 @@
|
||||
<ProjectReference Include="..\PipeMaze\PipeMaze.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Content Include="..\INPUT.txt">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
+50
-3
@@ -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>()
|
||||
{
|
||||
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<Pipe> GetLoop()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user