Skip to content

Commit

Permalink
Day10b: Solved
Browse files Browse the repository at this point in the history
  • Loading branch information
julemand101 committed Dec 10, 2024
1 parent 9830233 commit c209f78
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 23 deletions.
33 changes: 16 additions & 17 deletions lib/day10.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,31 @@
import 'dart:collection';
import 'dart:typed_data';

int solveA(List<String> input) {
final grid = Grid(input.first.length + 2, input.length + 2)
..setFromInput(input, padding: 1);
var sum = 0;
import 'package:collection/collection.dart';

for (final startingPosition in grid.zeroPositions) {
sum += reachTop(grid, startingPosition, 0, HashSet());
}
int solveA(List<String> input) => solve(input, partB: false);
int solveB(List<String> input) => solve(input, partB: true);

return sum;
}
int solve(List<String> input, {required bool partB}) {
final grid = Grid(input.first.length + 2, input.length + 2)
..setFromInput(input, padding: 1);

int solveB(List<String> input) {
return 0;
return grid.zeroPositions
.map((startingPosition) =>
reachTop(grid, startingPosition, 0, partB ? null : HashSet()))
.sum;
}

final Point up = Point(0, -1);
final Point right = Point(1, 0);
final Point down = Point(0, 1);
final Point left = Point(-1, 0);
const Point up = Point(0, -1);
const Point right = Point(1, 0);
const Point down = Point(0, 1);
const Point left = Point(-1, 0);

int reachTop(
Grid grid,
Point currentPosition,
int nextLevel,
Set<Point> visitedBefore,
Set<Point>? visitedBefore,
) {
final currentValue = grid.getByPoint(currentPosition);

Expand All @@ -38,7 +37,7 @@ int reachTop(
}

if (currentValue == 9) {
return visitedBefore.add(currentPosition) ? 1 : 0;
return (visitedBefore?.add(currentPosition) ?? true) ? 1 : 0;
}

return reachTop(grid, currentPosition + up, nextLevel + 1, visitedBefore) +
Expand Down
12 changes: 6 additions & 6 deletions test/day10_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ void main() {
group('Part Two', () {
test('Example 1', () {
expect(
solveA(r'''
solveB(r'''
.....0.
..4321.
..5..2.
Expand All @@ -99,7 +99,7 @@ void main() {
});
test('Example 2', () {
expect(
solveA(r'''
solveB(r'''
..90..9
...1.98
...2..7
Expand All @@ -113,7 +113,7 @@ void main() {
});
test('Example 3', () {
expect(
solveA(r'''
solveB(r'''
012345
123456
234567
Expand All @@ -126,7 +126,7 @@ void main() {
});
test('Example 4', () {
expect(
solveA(r'''
solveB(r'''
89010123
78121874
87430965
Expand All @@ -140,7 +140,7 @@ void main() {
equals(81));
});
test('Solution', () {
expect(solveB(input), equals(-1));
expect(solveB(input), equals(1302));
});
}, skip: true);
});
}

0 comments on commit c209f78

Please sign in to comment.