-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
530115d
commit d6a28be
Showing
6 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
1 | ||
10 | ||
100 | ||
2024 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
} |