Skip to content

Commit

Permalink
Solution for 2024-12-10 part 2
Browse files Browse the repository at this point in the history
I had already stumbled upon it by accident while working on part 1.
  • Loading branch information
tetsuo13 committed Dec 17, 2024
1 parent b84fda8 commit ed13e3d
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 42 deletions.
28 changes: 16 additions & 12 deletions src/AdventOfCode/Calendar/Year2024/Day10/Solution.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ public class Solution : BaseSolution

private int[,] _map;

private enum Measure { Score, Rating }

public override object Run(RunMode runMode)
{
InitializeMap(ReadInput());

return runMode switch
{
RunMode.PartOne => SumTrailheadScores(),
RunMode.PartTwo => 0,
RunMode.PartOne => SumTrailheads(Measure.Score),
RunMode.PartTwo => SumTrailheads(Measure.Rating),
_ => throw new ArgumentOutOfRangeException(nameof(runMode))
};
}
Expand Down Expand Up @@ -49,7 +51,7 @@ private void InitializeMap(string[] inputLines)
}
}

private int SumTrailheadScores()
private int SumTrailheads(Measure measure)
{
var sum = 0;

Expand All @@ -63,49 +65,51 @@ private int SumTrailheadScores()
}

List<Point> trailends = [];
DoHikingTrail(row, column, trailends);
DoHikingTrail(row, column, trailends, measure);
sum += trailends.Count;
}
}

return sum;
}

private void DoHikingTrail(int row, int column, List<Point> trailends)
private void DoHikingTrail(int row, int column, List<Point> trailends, Measure measure)
{
var height = _map[row, column];

if (height == Trailend)
{
var trailend = new Point(row, column);

// *Unique* trailends!
if (!trailends.Contains(trailend))
switch (measure)
{
trailends.Add(trailend);
case Measure.Score when !trailends.Contains(trailend): // *Unique* trailends!
case Measure.Rating:
trailends.Add(trailend);
break;
}

return;
}

if (row > 0 && _map[row - 1, column] == height + 1)
{
DoHikingTrail(row - 1, column, trailends);
DoHikingTrail(row - 1, column, trailends, measure);
}

if (row < _map.GetLength(0) - 1 && _map[row + 1, column] == height + 1)
{
DoHikingTrail(row + 1, column, trailends);
DoHikingTrail(row + 1, column, trailends, measure);
}

if (column > 0 && _map[row, column - 1] == height + 1)
{
DoHikingTrail(row, column - 1, trailends);
DoHikingTrail(row, column - 1, trailends, measure);
}

if (column < _map.GetLength(1) - 1 && _map[row, column + 1] == height + 1)
{
DoHikingTrail(row, column + 1, trailends);
DoHikingTrail(row, column + 1, trailends, measure);
}
}
}
99 changes: 69 additions & 30 deletions tests/AdventOfCode.Tests/Calendar/Year2024/Day10Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,49 @@ namespace AdventOfCode.Tests.Calendar.Year2024;

public class Day10Tests
{
[Theory]
[InlineData(4, RunMode.PartOne)]
[InlineData(13, RunMode.PartTwo)]
public void Example1(int expected, RunMode runMode)
{
string[] input =
[
"..90..9",
"...1.98",
"...2..7",
"6543456",
"765.987",
"876....",
"987...."
];

var solution = new SolutionTestWrapper<Solution>(input);
Assert.Equal(expected, solution.Run(runMode));
}

[Theory]
[InlineData(36, RunMode.PartOne)]
[InlineData(81, RunMode.PartTwo)]
public void Example2(int expected, RunMode runMode)
{
string[] input =
[
"89010123",
"78121874",
"87430965",
"96549874",
"45678903",
"32019012",
"01329801",
"10456732"
];

var solution = new SolutionTestWrapper<Solution>(input);
Assert.Equal(expected, solution.Run(runMode));
}

[Fact]
public void Part_Example1()
public void Part1_Example1()
{
string[] input =
[
Expand All @@ -22,7 +63,7 @@ public void Part_Example1()
}

[Fact]
public void Part_Example2()
public void Part1_Example2()
{
string[] input =
[
Expand All @@ -40,57 +81,55 @@ public void Part_Example2()
}

[Fact]
public void Part_Example3()
public void Part1_Example3()
{
string[] input =
[
"..90..9",
"...1.98",
"...2..7",
"6543456",
"765.987",
"876....",
"987...."
"10..9..",
"2...8..",
"3...7..",
"4567654",
"...8..3",
"...9..2",
".....01"
];

var solution = new SolutionTestWrapper<Solution>(input);
Assert.Equal(4, solution.Run(RunMode.PartOne));
Assert.Equal(3, solution.Run(RunMode.PartOne));
}

[Fact]
public void Part_Example4()
public void Part2_Example1()
{
string[] input =
[
"10..9..",
"2...8..",
"3...7..",
"4567654",
"...8..3",
"...9..2",
".....01"
".....0.",
"..4321.",
"..5..2.",
"..6543.",
"..7..4.",
"..8765.",
"..9...."
];

var solution = new SolutionTestWrapper<Solution>(input);
Assert.Equal(3, solution.Run(RunMode.PartOne));
Assert.Equal(3, solution.Run(RunMode.PartTwo));
}

[Fact]
public void Part_Example5()
public void Part2_Example2()
{
string[] input =
[
"89010123",
"78121874",
"87430965",
"96549874",
"45678903",
"32019012",
"01329801",
"10456732"
"012345",
"123456",
"234567",
"345678",
"4.6789",
"56789."
];

var solution = new SolutionTestWrapper<Solution>(input);
Assert.Equal(36, solution.Run(RunMode.PartOne));
Assert.Equal(227, solution.Run(RunMode.PartTwo));
}
}

0 comments on commit ed13e3d

Please sign in to comment.