Skip to content

Commit

Permalink
day(18): RAM Run 🧝
Browse files Browse the repository at this point in the history
  • Loading branch information
adamrodger committed Dec 18, 2024
1 parent 7447e0d commit 01826eb
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 1 deletion.
71 changes: 71 additions & 0 deletions src/AdventOfCode/Day18.cs
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;
}
}
}
2 changes: 1 addition & 1 deletion src/AdventOfCode/Utilities/Graph.cs
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
Expand Down
Binary file added src/AdventOfCode/inputs/day18.txt
Binary file not shown.
47 changes: 47 additions & 0 deletions tests/AdventOfCode.Tests/Day18Tests.cs
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);
}
}
}

0 comments on commit 01826eb

Please sign in to comment.