Skip to content

Commit

Permalink
Day 22 Part 1 solution
Browse files Browse the repository at this point in the history
  • Loading branch information
dancarroll committed Dec 22, 2024
1 parent 530115d commit d6a28be
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 0 deletions.
2 changes: 2 additions & 0 deletions bin/aoc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import 'day18.dart' as day18;
import 'day19.dart' as day19;
import 'day20.dart' as day20;
import 'day21.dart' as day21;
import 'day22.dart' as day22;

void main(List<String> arguments) async {
print('');
Expand All @@ -44,6 +45,7 @@ void main(List<String> arguments) async {
day19.main,
day20.main,
day21.main,
day22.main,
]) {
await day(arguments);
print('');
Expand Down
10 changes: 10 additions & 0 deletions bin/day22.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import 'package:aoc_2024/lib.dart';
import 'package:aoc_2024/day22/part_1.dart' as part1;

Future<void> main(List<String> arguments) async {
await runDay(
day: Day.day22,
part1: part1.calculate,
part2: (_) => Future.value(0),
);
}
28 changes: 28 additions & 0 deletions lib/day22/part_1.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import 'dart:io';

import 'package:collection/collection.dart';

import 'shared.dart';

/// --- Day 22: Monkey Market ---
///
/// Determine the numbers generated by monkeys at an auction.
/// Each monkey starts with a secret number, and generates their
/// next number based on a defined set of operations.
///
/// Return the sum of all secret numbers after 2000 iterations.
Future<int> calculate(File file) async {
final data = await loadData(file);

return data.map((s) => _simulate(s, 2000)).sum;
}

/// Generate the secret number after [iterations], starting at
/// the given [starting] secret number.
int _simulate(int starting, int iterations) {
int secret = starting;
for (int i = 0; i < iterations; i++) {
secret = nextSecret(secret);
}
return secret;
}
15 changes: 15 additions & 0 deletions lib/day22/shared.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import 'dart:io';

/// Calculates the next secret number after the given [secret].
int nextSecret(int secret) {
int next = secret;
next = ((next * 64) ^ next) % 16777216;
next = ((next ~/ 32) ^ next) % 16777216;
return ((next * 2048) ^ next) % 16777216;
}

/// Loads the list of starting secret numbers.
Future<List<int>> loadData(File file) async {
final lines = await file.readAsLines();
return lines.map(int.parse).toList();
}
4 changes: 4 additions & 0 deletions resources/sample_data/day22.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
1
10
100
2024
25 changes: 25 additions & 0 deletions test/day22_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import 'package:aoc_2024/lib.dart';
import 'package:aoc_2024/day22/part_1.dart' as part1;
import 'package:test/test.dart';

void main() {
final day = Day.day22;

group('sample data', tags: 'sample-data', () {
final resources = Resources.sample;
final file = resources.file(day);

test('part1', () async {
expect(await part1.calculate(file), 37327623);
});
});

group('real data', tags: 'real-data', () {
final resources = Resources.real;
final file = resources.file(day);

test('part1', () async {
expect(await part1.calculate(file), 15608699004);
});
});
}

0 comments on commit d6a28be

Please sign in to comment.