From 6ab4dc5ef88ad5dfba3583a6b6ca99594cce7d91 Mon Sep 17 00:00:00 2001 From: Sander in 't Hout Date: Wed, 11 Dec 2024 17:00:52 +0100 Subject: [PATCH] Add AoC 2024 day 11 part 1 solver --- assets/advent_of_code/2024_11_sample.txt | 1 + .../advent_of_code/2024/_all_solvers.dart | 1 + .../advent_of_code/2024/day_11_solver.dart | 37 +++++++++++++++++++ lib/solvers/helpers/extensions.dart | 3 ++ 4 files changed, 42 insertions(+) create mode 100644 assets/advent_of_code/2024_11_sample.txt create mode 100644 lib/solvers/advent_of_code/2024/day_11_solver.dart diff --git a/assets/advent_of_code/2024_11_sample.txt b/assets/advent_of_code/2024_11_sample.txt new file mode 100644 index 0000000..528f9d5 --- /dev/null +++ b/assets/advent_of_code/2024_11_sample.txt @@ -0,0 +1 @@ +125 17 \ No newline at end of file diff --git a/lib/solvers/advent_of_code/2024/_all_solvers.dart b/lib/solvers/advent_of_code/2024/_all_solvers.dart index 4d51f0e..31e8465 100644 --- a/lib/solvers/advent_of_code/2024/_all_solvers.dart +++ b/lib/solvers/advent_of_code/2024/_all_solvers.dart @@ -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'; diff --git a/lib/solvers/advent_of_code/2024/day_11_solver.dart b/lib/solvers/advent_of_code/2024/day_11_solver.dart new file mode 100644 index 0000000..778b7c1 --- /dev/null +++ b/lib/solvers/advent_of_code/2024/day_11_solver.dart @@ -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 initialArrangement = input.split(' ').map(int.parse).toList(); + + // Part 1 + List currentArrangement = initialArrangement.toList(); + for (int blinked = 0; blinked < 25; blinked++) { + List tmpArrangement = []; + for (int stone in currentArrangement) { + if (stone == 0) { + tmpArrangement.add(1); + } else { + List 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}'; + } + +} diff --git a/lib/solvers/helpers/extensions.dart b/lib/solvers/helpers/extensions.dart index e7dca28..17881b5 100644 --- a/lib/solvers/helpers/extensions.dart +++ b/lib/solvers/helpers/extensions.dart @@ -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 get digits => toString().characters.map(int.parse).toList(); + int lcm(int other) => (this * other).abs() ~/ gcd(other); }