Skip to content

Commit

Permalink
Write a util function to determine the valid next steps, and share it…
Browse files Browse the repository at this point in the history
… between parts 1 and 2
  • Loading branch information
dancarroll committed Dec 11, 2024
1 parent 731fc8f commit 8d563e7
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 43 deletions.
25 changes: 6 additions & 19 deletions lib/day10/part_1.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,12 @@ Set<Point> _summitsReachableFromPoint(
Set<Point> summits = {};
visitedPoints.add(point);

for (final record in [(1, 0), (-1, 0), (0, 1), (0, -1)]) {
final newX = point.x + record.$1;
final newY = point.y + record.$2;

if (newX < 0 ||
newY < 0 ||
newX >= topoMap.height ||
newY >= topoMap.height) {
continue;
}

final nextPoint = topoMap.getPoint(newX, newY);
if (nextPoint.isGradualStepFromPoint(point)) {
if (nextPoint.isSummit) {
summits.add(nextPoint);
} else {
summits.addAll(
_summitsReachableFromPoint(topoMap, nextPoint, visitedPoints));
}
for (final nextPoint in topoMap.getValidNextSteps(point)) {
if (nextPoint.isSummit) {
summits.add(nextPoint);
} else {
summits.addAll(
_summitsReachableFromPoint(topoMap, nextPoint, visitedPoints));
}
}

Expand Down
33 changes: 9 additions & 24 deletions lib/day10/part_2.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Future<int> calculate(File file) async {

int sum = 0;
for (final trailhead in topoMap.trailheads) {
final summits = _summitsReachableFromPoint(topoMap, trailhead, {});
final summits = _summitsReachableFromPoint(topoMap, trailhead);
sum += summits.length;
}

Expand All @@ -23,31 +23,16 @@ Future<int> calculate(File file) async {
/// Finds all of the summits reachable from this point. Because we are not
/// tracking visited points in this DFS algorithm, this will return the same
/// point multiple times if it is reachable via multiple paths. Since there is a
/// requirement of the next elevation increasing by exactly one, there is no risk
/// of backtracking and hitting endless cycles.
List<Point> _summitsReachableFromPoint(
TopographicMap topoMap, Point point, Set<Point> visitedPoints) {
/// requirement of the next elevation increasing by exactly one, there is no
/// risk of backtracking and hitting endless cycles.
List<Point> _summitsReachableFromPoint(TopographicMap topoMap, Point point) {
List<Point> summits = [];

for (final record in [(1, 0), (-1, 0), (0, 1), (0, -1)]) {
final newX = point.x + record.$1;
final newY = point.y + record.$2;

if (newX < 0 ||
newY < 0 ||
newX >= topoMap.height ||
newY >= topoMap.height) {
continue;
}

final nextPoint = topoMap.getPoint(newX, newY);
if (nextPoint.isGradualStepFromPoint(point)) {
if (nextPoint.isSummit) {
summits.add(nextPoint);
} else {
summits.addAll(
_summitsReachableFromPoint(topoMap, nextPoint, visitedPoints));
}
for (final nextPoint in topoMap.getValidNextSteps(point)) {
if (nextPoint.isSummit) {
summits.add(nextPoint);
} else {
summits.addAll(_summitsReachableFromPoint(topoMap, nextPoint));
}
}

Expand Down
24 changes: 24 additions & 0 deletions lib/day10/shared.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,30 @@ final class TopographicMap {
return map[r][c];
}

/// Returns the set of valid next steps from the given point.
///
/// A valid step is one step along a cardinal directions, in the bounds of
/// the map, and at an elevation exactly 1 greater than the given point.
Set<Point> getValidNextSteps(Point point) {
Set<Point> nextSteps = {};

for (final record in [(1, 0), (-1, 0), (0, 1), (0, -1)]) {
final newX = point.x + record.$1;
final newY = point.y + record.$2;

if (newX < 0 || newY < 0 || newX >= height || newY >= height) {
continue;
}

final nextPoint = getPoint(newX, newY);
if (nextPoint.isGradualStepFromPoint(point)) {
nextSteps.add(nextPoint);
}
}

return nextSteps;
}

/// Returns all of the trailheads in this map.
Iterable<Point> get trailheads => map.flattened.where((p) => p.isTrailhead);
}
Expand Down

0 comments on commit 8d563e7

Please sign in to comment.