-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d9d10c7
commit b70d098
Showing
5 changed files
with
68 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters