-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7447e0d
commit 01826eb
Showing
4 changed files
with
119 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
using System.Linq; | ||
using AdventOfCode.Utilities; | ||
|
||
namespace AdventOfCode | ||
{ | ||
/// <summary> | ||
/// Solver for Day 18 | ||
/// </summary> | ||
public class Day18 | ||
{ | ||
public int Part1(string[] input, int width = 70) | ||
{ | ||
var graph = BuildGraph(input, width, 1024); | ||
return graph.GetShortestPath((0, 0), (width, width)).Last().distance; | ||
} | ||
|
||
public string Part2(string[] input, int width = 70) | ||
{ | ||
int lowestValid = 1024; // we know there's definitely a valid path here from p1 | ||
int highestInvalid = input.Length; // we know there's no valid path here otherwise there is no answer | ||
|
||
while (highestInvalid - lowestValid > 1) | ||
{ | ||
int mid = (highestInvalid + lowestValid) / 2; | ||
|
||
Graph<Point2D> graph = BuildGraph(input, width, mid); | ||
|
||
if (graph.GetShortestPath((0, 0), (width, width)) == null) | ||
{ | ||
highestInvalid = mid; | ||
} | ||
else | ||
{ | ||
lowestValid = mid; | ||
} | ||
} | ||
|
||
return input[highestInvalid - 1]; | ||
} | ||
|
||
private static Graph<Point2D> BuildGraph(string[] input, int width, int wallCount) | ||
{ | ||
var graph = new Graph<Point2D>(Graph<Point2D>.ManhattanDistanceHeuristic); | ||
|
||
var walls = input.Select(i => i.Numbers<int>()) | ||
.Select(n => new Point2D(n[0], n[1])) | ||
.Take(wallCount) | ||
.ToHashSet(); | ||
|
||
for (int y = 0; y <= width; y++) | ||
{ | ||
for (int x = 0; x <= width; x++) | ||
{ | ||
Point2D point = (x, y); | ||
|
||
foreach (Point2D adjacent in point.Adjacent4() | ||
.Where(p => p.X >= 0 | ||
&& p.X <= width | ||
&& p.Y >= 0 | ||
&& p.Y <= width | ||
&& !walls.Contains(p))) | ||
{ | ||
graph.AddVertex(point, adjacent); | ||
} | ||
} | ||
} | ||
|
||
return graph; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
using System; | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace AdventOfCode.Utilities | ||
|
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using System.IO; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
|
||
namespace AdventOfCode.Tests | ||
{ | ||
public class Day18Tests | ||
{ | ||
private readonly ITestOutputHelper output; | ||
private readonly Day18 solver; | ||
|
||
public Day18Tests(ITestOutputHelper output) | ||
{ | ||
this.output = output; | ||
this.solver = new Day18(); | ||
} | ||
|
||
private static string[] GetRealInput() | ||
{ | ||
string[] input = File.ReadAllLines("inputs/day18.txt"); | ||
return input; | ||
} | ||
|
||
[Fact] | ||
public void Part1_RealInput_ProducesCorrectResponse() | ||
{ | ||
var expected = 282; | ||
|
||
var result = solver.Part1(GetRealInput()); | ||
output.WriteLine($"Day 18 - Part 1 - {result}"); | ||
|
||
Assert.Equal(expected, result); | ||
} | ||
|
||
[Fact] | ||
public void Part2_RealInput_ProducesCorrectResponse() | ||
{ | ||
var expected = "64,29"; | ||
|
||
var result = solver.Part2(GetRealInput()); | ||
output.WriteLine($"Day 18 - Part 2 - {result}"); | ||
|
||
Assert.Equal(expected, result); | ||
} | ||
} | ||
} |