Skip to content

Commit

Permalink
Write a util to time each part function, and
Browse files Browse the repository at this point in the history
refactor all existing solutions to use it. Add
a new entrypoint to run all days against both
sample and real data.
  • Loading branch information
dancarroll committed Dec 7, 2024
1 parent 1976c5f commit de924a9
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 25 deletions.
15 changes: 15 additions & 0 deletions bin/aoc.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import 'day1.dart' as day1;
import 'day2.dart' as day2;
import 'day3.dart' as day3;
import 'day4.dart' as day4;
import 'day5.dart' as day5;
import 'day6.dart' as day6;

void main(List<String> arguments) async {
print('');
for (final day in [day1.main, day2.main, day3.main, day4.main, day5.main, day6.main]) {
await day(arguments);
print('');
}
}

7 changes: 2 additions & 5 deletions bin/day1.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ import 'package:aoc_2024/lib.dart';
import 'package:aoc_2024/day1/part_1.dart' as part1;
import 'package:aoc_2024/day1/part_2.dart' as part2;

void main(List<String> arguments) async {
print('Advent of Code - Day 1');
print('Part 1: ${await part1.calculate(Resources.real)}');
print('Part 2: ${await part2.calculate(Resources.real)}');
Future<void> main(List<String> arguments) async {
await runDay(day: 1, part1: part1.calculate, part2: part2.calculate);
}

6 changes: 2 additions & 4 deletions bin/day2.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import 'package:aoc_2024/lib.dart';
import 'package:aoc_2024/day2/part_1.dart' as part1;
import 'package:aoc_2024/day2/part_2.dart' as part2;

void main(List<String> arguments) async {
print('Advent of Code - Day 2');
print('Part 1: ${await part1.calculate(Resources.real)}');
print('Part 2: ${await part2.calculate(Resources.real)}');
Future<void> main(List<String> arguments) async {
await runDay(day: 2, part1: part1.calculate, part2: part2.calculate);
}
6 changes: 2 additions & 4 deletions bin/day3.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import 'package:aoc_2024/lib.dart';
import 'package:aoc_2024/day3/part_1.dart' as part1;
import 'package:aoc_2024/day3/part_2.dart' as part2;

void main(List<String> arguments) async {
print('Advent of Code - Day 3');
print('Part 1: ${await part1.calculate(Resources.real)}');
print('Part 2: ${await part2.calculate(Resources.real)}');
Future<void> main(List<String> arguments) async {
await runDay(day: 3, part1: part1.calculate, part2: part2.calculate);
}
6 changes: 2 additions & 4 deletions bin/day4.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import 'package:aoc_2024/lib.dart';
import 'package:aoc_2024/day4/part_1.dart' as part1;
import 'package:aoc_2024/day4/part_2.dart' as part2;

void main(List<String> arguments) async {
print('Advent of Code - Day 4');
print('Part 1: ${await part1.calculate(Resources.real)}');
print('Part 2: ${await part2.calculate(Resources.real)}');
Future<void> main(List<String> arguments) async {
await runDay(day: 4, part1: part1.calculate, part2: part2.calculate);
}
6 changes: 2 additions & 4 deletions bin/day5.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import 'package:aoc_2024/lib.dart';
import 'package:aoc_2024/day5/part_1.dart' as part1;
import 'package:aoc_2024/day5/part_2.dart' as part2;

void main(List<String> arguments) async {
print('Advent of Code - Day 4');
print('Part 1: ${await part1.calculate(Resources.real)}');
print('Part 2: ${await part2.calculate(Resources.real)}');
Future<void> main(List<String> arguments) async {
await runDay(day: 5, part1: part1.calculate, part2: part2.calculate);
}
6 changes: 2 additions & 4 deletions bin/day6.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import 'package:aoc_2024/lib.dart';
import 'package:aoc_2024/day6/part_1.dart' as part1;
import 'package:aoc_2024/day6/part_2.dart' as part2;

void main(List<String> arguments) async {
print('Advent of Code - Day 4');
print('Part 1: ${await part1.calculate(Resources.real)}');
print('Part 2: ${await part2.calculate(Resources.real)}');
Future<void> main(List<String> arguments) async {
await runDay(day: 6, part1: part1.calculate, part2: part2.calculate);
}
1 change: 1 addition & 0 deletions lib/lib.dart
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export 'shared/resources.dart';
export 'shared/runner.dart';
3 changes: 3 additions & 0 deletions lib/shared/resources.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,7 @@ final class Resources {
};
return File(path.join('resources', folder, '${day.name}.txt'));
}

@override
String toString() => type.name;
}
27 changes: 27 additions & 0 deletions lib/shared/runner.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import 'resources.dart';

typedef DayFunction = Future<int> Function(Resources);

final _stopwatch = Stopwatch();

/// Runs both parts for a day, and prints their output as well as the
/// elapsed time to run each part.
Future<void> runDay(
{required final int day,
required final DayFunction part1,
required final DayFunction part2}) async {
print('Advent of Code - Day $day');
for (final resource in [Resources.sample, Resources.real]) {
print('- $resource data:');

for (final part in [(1, part1), (2, part2)]) {
_stopwatch.reset();
_stopwatch.start();
final result = await part.$2(resource);
_stopwatch.stop();

print(
' - Part ${part.$1}: $result (${_stopwatch.elapsedMilliseconds}ms)');
}
}
}

0 comments on commit de924a9

Please sign in to comment.