From b418af8be11c8f7a3584e23661ae9474c621f618 Mon Sep 17 00:00:00 2001 From: Jacob Bang Date: Wed, 11 Dec 2024 06:30:26 +0100 Subject: [PATCH] Day11a: Solved --- lib/day11.dart | 43 +++++++++++++++++++++++++++++++++++++++++++ test/data | 2 +- test/day11_test.dart | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 lib/day11.dart create mode 100644 test/day11_test.dart diff --git a/lib/day11.dart b/lib/day11.dart new file mode 100644 index 0000000..4c45692 --- /dev/null +++ b/lib/day11.dart @@ -0,0 +1,43 @@ +// --- Day 11: Plutonian Pebbles --- +// https://adventofcode.com/2024/day/11 + +import 'dart:collection'; + +final class Stone extends LinkedListEntry { + int value; + + Stone(this.value); +} + +int solveA(String input, {int blink = 25}) => solve(input, blink: blink); +int solveB(String input) => solve(input, blink: 75); + +int solve(String input, {int blink = 25}) { + final stones = LinkedList(); + + for (final stoneValue in input.split(' ').map(int.parse)) { + stones.add(Stone(stoneValue)); + } + + for (var i = 0; i < blink; i++) { + Stone? stone = stones.first; + + while (stone != null) { + if (stone.value == 0) { + stone.value = 1; + } else if (stone.value.toString() case final numberString + when numberString.length % 2 == 0) { + stone.insertBefore(Stone( + int.parse(numberString.substring(0, numberString.length ~/ 2)))); + stone.value = + int.parse(numberString.substring(numberString.length ~/ 2)); + } else { + stone.value *= 2024; + } + + stone = stone.next; + } + } + + return stones.length; +} diff --git a/test/data b/test/data index 2a0a6b8..02ed5e7 160000 --- a/test/data +++ b/test/data @@ -1 +1 @@ -Subproject commit 2a0a6b8ae3a3cc26a00c5ebfab84e7c9f1fc0c58 +Subproject commit 02ed5e74970b5a38e9bfbd4ed1e0f3bdb9a35ab4 diff --git a/test/day11_test.dart b/test/day11_test.dart new file mode 100644 index 0000000..9717a69 --- /dev/null +++ b/test/day11_test.dart @@ -0,0 +1,33 @@ +// --- Day 11: Plutonian Pebbles --- +// https://adventofcode.com/2024/day/11 + +import 'dart:io'; +import 'package:advent_of_code_2024/day11.dart'; +import 'package:test/test.dart'; + +final input = File('test/data/day11.txt').readAsLinesSync().first; + +void main() { + group('Part One', () { + test('Example 1', () { + expect(solveA('0 1 10 99 999', blink: 1), equals(7)); + }); + test('Example 2', () { + expect(solveA('125 17', blink: 1), equals(3)); + }); + test('Example 3', () { + expect(solveA('125 17', blink: 6), equals(22)); + }); + test('Example 4', () { + expect(solveA('125 17'), equals(55312)); + }); + test('Solution', () { + expect(solveA(input), equals(193607)); + }); + }); + group('Part Two', () { + test('Solution', () { + expect(solveB(input), equals(-1)); + }); + }, skip: true); +}