Skip to content

Commit

Permalink
Add AoC 2024 day 11 part 1 solver
Browse files Browse the repository at this point in the history
  • Loading branch information
h3x4d3c1m4l committed Dec 11, 2024
1 parent 55f3b40 commit 6ab4dc5
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions assets/advent_of_code/2024_11_sample.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
125 17
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 @@ -4,3 +4,4 @@ export 'day_03_solver.dart';
export 'day_04_solver.dart';
export 'day_05_solver.dart';
export 'day_06_solver.dart';
export 'day_11_solver.dart';
37 changes: 37 additions & 0 deletions lib/solvers/advent_of_code/2024/day_11_solver.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import 'package:h3x_devtools/solvers/advent_of_code/2024/aoc_2024_solver.dart';

class Day11Solver extends AdventOfCode2024Solver {

@override
final int dayNumber = 11;

@override
String getSolution(String input) {
List<int> initialArrangement = input.split(' ').map(int.parse).toList();

// Part 1
List<int> currentArrangement = initialArrangement.toList();
for (int blinked = 0; blinked < 25; blinked++) {
List<int> tmpArrangement = [];
for (int stone in currentArrangement) {
if (stone == 0) {
tmpArrangement.add(1);
} else {
List<int> stoneDigits = stone.digits;
if (stoneDigits.length.isEven) {
String leftNumber = stoneDigits.take(stoneDigits.length ~/ 2).join();
String rightNumber = stoneDigits.skip(stoneDigits.length ~/ 2).join();
tmpArrangement..add(int.parse(leftNumber))..add(int.parse(rightNumber));
} else {
tmpArrangement.add(stone * 2024);
}
}
}

currentArrangement = tmpArrangement;
}

return 'Stone count after blinking 25 times: ${currentArrangement.length}';
}

}
3 changes: 3 additions & 0 deletions lib/solvers/helpers/extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ extension IntExtension on int {
bool get isDigit => this >= 48 && this <= 57;
bool get isDigit1to9 => this >= 49 && this <= 58;
bool get isDot => this == 46;
int get digitCount => toString().length;
List<int> get digits => toString().characters.map(int.parse).toList();

int lcm(int other) => (this * other).abs() ~/ gcd(other);

}
Expand Down

0 comments on commit 6ab4dc5

Please sign in to comment.