Skip to content

Commit

Permalink
day 10 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
KaterynaKateryna committed Dec 11, 2024
1 parent df6e196 commit 4ae1d29
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions AdventOfCode/Day10.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public override ValueTask<string> Solve_1()
{
if (_map[i][j] == 0)
{
totalScore += GetTrailheadScore(i, j);
totalScore += GetTrailheadScore(i, j, getRating: false);
}
}
}
Expand All @@ -29,16 +29,28 @@ public override ValueTask<string> Solve_1()

public override ValueTask<string> Solve_2()
{
return new("");
int totalScore = 0;
for (int i = 0; i < _map.Length; ++i)
{
for (int j = 0; j < _map[0].Length; ++j)
{
if (_map[i][j] == 0)
{
totalScore += GetTrailheadScore(i, j, getRating: true);
}
}
}

return new(totalScore.ToString());
}

private int GetTrailheadScore(int i, int j)
private int GetTrailheadScore(int i, int j, bool getRating)
{
Point start = new Point(i, j);
return GetTrailheadScore(start, new HashSet<Point>() { start }, new HashSet<Point>());
return GetTrailheadScore(start, new HashSet<Point>() { start }, new HashSet<Point>(), getRating);
}

private int GetTrailheadScore(Point current, HashSet<Point> visited, HashSet<Point> ninePositionsreached)
private int GetTrailheadScore(Point current, HashSet<Point> visited, HashSet<Point> ninePositionsreached, bool getRating)
{
int currentValue = _map[current.I][current.J];
if (currentValue == 9)
Expand All @@ -47,7 +59,7 @@ private int GetTrailheadScore(Point current, HashSet<Point> visited, HashSet<Poi
{
return 1;
}
return 0;
return getRating ? 1 : 0;
}

int score = 0;
Expand All @@ -57,7 +69,7 @@ private int GetTrailheadScore(Point current, HashSet<Point> visited, HashSet<Poi
if (neighbour != null && _map[neighbour.I][neighbour.J] == currentValue + 1)
{
visited.Add(neighbour);
score += GetTrailheadScore(neighbour, visited, ninePositionsreached);
score += GetTrailheadScore(neighbour, visited, ninePositionsreached, getRating);
visited.Remove(neighbour);
}
}
Expand Down

0 comments on commit 4ae1d29

Please sign in to comment.