Skip to content

Commit

Permalink
Day14: Removal of unneeded HashMap
Browse files Browse the repository at this point in the history
We don't need to split the tracking of rocks and sand points since we just need to know the amount of rocks we started with and can then subtract this later.
  • Loading branch information
julemand101 committed Jul 4, 2023
1 parent 4d72344 commit e5567fb
Showing 1 changed file with 17 additions and 21 deletions.
38 changes: 17 additions & 21 deletions lib/day14.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,31 @@ import 'dart:collection';
import 'dart:math';

int solveA(Iterable<String> input) {
final HashSet<Point<int>> rockPoints = parseRocks(input);

final int bottomY = rockPoints.reduce((a, b) => a.y > b.y ? a : b).y;
final HashSet<Point<int>> sandPoints = HashSet();
// A point might be filled with either a rock or sand. In the beginning, we
// only have rocks. Since we later need to only count the sand, we keep a
// count of how many rocks there are so we can subtract that later.
final HashSet<Point<int>> filledPoints = parseRocks(input);
final int numberOfRocks = filledPoints.length;
final int bottomY = filledPoints.reduce((a, b) => a.y > b.y ? a : b).y;
const sandSpawnPoint = Point<int>(500, 0);

bool isNotBlocked(Point<int> point) =>
!sandPoints.contains(point) && !rockPoints.contains(point);

outerLoop:
while (true) {
var x = sandSpawnPoint.x;

for (var y = sandSpawnPoint.y; y < bottomY; y++) {
final point = Point(x, y);

if (isNotBlocked(point.downOneStep)) {
if (!filledPoints.contains(point.downOneStep)) {
// Do nothing since we should just keep going directly down
} else if (isNotBlocked(point.oneStepDownAndToTheLeft)) {
} else if (!filledPoints.contains(point.oneStepDownAndToTheLeft)) {
// Move one step to the left
x--;
} else if (isNotBlocked(point.oneStepDownAndToTheRight)) {
} else if (!filledPoints.contains(point.oneStepDownAndToTheRight)) {
// Move one step to the right
x++;
} else {
sandPoints.add(point);
filledPoints.add(point);
continue outerLoop;
}
}
Expand All @@ -40,20 +39,17 @@ int solveA(Iterable<String> input) {
break;
}

return sandPoints.length;
return filledPoints.length - numberOfRocks;
}

int solveB(Iterable<String> input) {
final HashSet<Point<int>> rockPoints = parseRocks(input);

final int bottomY = rockPoints.reduce((a, b) => a.y > b.y ? a : b).y + 2;
final HashSet<Point<int>> sandPoints = HashSet();
final HashSet<Point<int>> filledPoints = parseRocks(input);
final int numberOfRocks = filledPoints.length;
final int bottomY = filledPoints.reduce((a, b) => a.y > b.y ? a : b).y + 2;
const sandSpawnPoint = Point<int>(500, 0);

bool isNotBlocked(Point<int> point) =>
!sandPoints.contains(point) &&
!rockPoints.contains(point) &&
point.y < bottomY;
!filledPoints.contains(point) && point.y < bottomY;

outerLoop:
while (isNotBlocked(sandSpawnPoint)) {
Expand All @@ -71,13 +67,13 @@ int solveB(Iterable<String> input) {
// Move one step to the right
x++;
} else {
sandPoints.add(point);
filledPoints.add(point);
continue outerLoop;
}
}
}

return sandPoints.length;
return filledPoints.length - numberOfRocks;
}

HashSet<Point<int>> parseRocks(Iterable<String> input) {
Expand Down

0 comments on commit e5567fb

Please sign in to comment.