Skip to content

Commit

Permalink
Day11a: Solved
Browse files Browse the repository at this point in the history
  • Loading branch information
julemand101 committed Dec 11, 2024
1 parent 6b96101 commit b418af8
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
43 changes: 43 additions & 0 deletions lib/day11.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// --- Day 11: Plutonian Pebbles ---
// https://adventofcode.com/2024/day/11

import 'dart:collection';

final class Stone extends LinkedListEntry<Stone> {
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<Stone>();

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;
}
2 changes: 1 addition & 1 deletion test/data
Submodule data updated from 2a0a6b to 02ed5e
33 changes: 33 additions & 0 deletions test/day11_test.dart
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit b418af8

Please sign in to comment.