Skip to content

Commit

Permalink
Add AoC 2024 day 14 part 1 solver
Browse files Browse the repository at this point in the history
  • Loading branch information
h3x4d3c1m4l committed Dec 16, 2024
1 parent d9d10c7 commit b70d098
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 3 deletions.
12 changes: 12 additions & 0 deletions assets/advent_of_code/2024_14_sample.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
p=0,4 v=3,-3
p=6,3 v=-1,-3
p=10,3 v=-1,2
p=2,0 v=2,-1
p=0,0 v=1,3
p=3,0 v=-2,-2
p=7,6 v=-1,-3
p=3,0 v=-1,-2
p=9,3 v=2,3
p=7,3 v=-1,2
p=2,4 v=2,-3
p=9,5 v=-3,-3
1 change: 1 addition & 0 deletions lib/solvers/advent_of_code/2024/_all_solvers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ export 'day_10_solver.dart';
export 'day_11_solver.dart';
export 'day_12_solver.dart';
export 'day_13_solver.dart';
export 'day_14_solver.dart';
export 'day_15_solver.dart';
52 changes: 52 additions & 0 deletions lib/solvers/advent_of_code/2024/day_14_solver.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import 'package:h3x_devtools/solvers/advent_of_code/2024/aoc_2024_solver.dart';

typedef _Robot = ({Coordinates start, int xPerSecond, int yPerSecond});

class Day14Solver extends AdventOfCode2024Solver {

@override
final int dayNumber = 14;

@override
String getSolution(String input) {
List<_Robot> robots = input.splitLines().map((line) {
List<String> lineSplit = line.split(' ');
List<int> startCoordinates = lineSplit[0].substring(2).split(',').map(int.parse).toList();
List<int> movementPerSecond = lineSplit[1].substring(2).split(',').map(int.parse).toList();
return (
start: Coordinates(startCoordinates[0], startCoordinates[1]),
xPerSecond: movementPerSecond[0],
yPerSecond: movementPerSecond[1],
);
}).toList();

// Keep into account the example has a smaller area.
int fieldWidth = robots.length == 12 ? 11 : 101;
int fieldHeight = robots.length == 12 ? 7 : 103;

// Part 1
int qLeftTop = 0, qRightTop = 0, qLeftBottom = 0, qRightBottom = 0;
for (_Robot robot in robots) {
int endX = (robot.start.x + (robot.xPerSecond * 100)) % fieldWidth;
int endY = (robot.start.y + (robot.yPerSecond * 100)) % fieldHeight;

if (endX < (fieldWidth - 1) ~/ 2) {
if (endY < (fieldHeight - 1) ~/ 2) {
qLeftTop++;
} else if (endY > (fieldHeight - 1) ~/ 2) {
qRightTop++;
}
} else if (endX > (fieldWidth - 1) ~/ 2) {
if (endY < (fieldHeight - 1) ~/ 2) {
qLeftBottom++;
} else if (endY > (fieldHeight - 1) ~/ 2) {
qRightBottom++;
}
}
}
int safetyScore = qLeftTop * qRightTop * qLeftBottom * qRightBottom;

return 'Safety score: $safetyScore';
}

}
4 changes: 2 additions & 2 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -992,5 +992,5 @@ packages:
source: hosted
version: "3.1.2"
sdks:
dart: ">=3.6.0-0 <4.0.0"
flutter: ">=3.24.0"
dart: ">=3.6.0 <4.0.0"
flutter: ">=3.27.0"
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ publish_to: 'none'
version: 1.0.0+1

environment:
sdk: '>=3.5.0 <4.0.0'
sdk: '>=3.6.0'

dependencies:
# Flutter framework
Expand Down

0 comments on commit b70d098

Please sign in to comment.