Files
adventofcode2025/lib/day1/command.dart
2025-12-01 16:51:50 -06:00

90 lines
2.1 KiB
Dart

import "dart:convert";
import "dart:io";
import "package:args/command_runner.dart";
enum Direction { left, right }
class Rotation {
Direction direction;
int amount;
Rotation(this.direction, this.amount);
@override
String toString() {
return '{ direction: ${direction.name}, amount: $amount }';
}
}
Future<List<Rotation>> parseInputFile(String filePath) async {
var inputFile = File(filePath);
var openFile = inputFile.openRead();
var lines = openFile.transform(utf8.decoder).transform(LineSplitter());
List<Rotation> inputList = [];
await for (var line in lines) {
var dir = line.substring(0, 1);
var amount = int.parse(line.substring(1, line.length));
inputList.add(
Rotation(dir == 'L' ? Direction.left : Direction.right, amount),
);
}
return inputList;
}
class Day1Command extends Command {
// The [name] and [description] properties must be defined by every
// subclass.
@override
final name = "day1";
@override
final description = "Run Advent of Code 2025 Day 1";
Day1Command() {
// we can add command specific arguments here.
// [argParser] is automatically created by the parent class.
}
// [run] may also return a Future.
@override
Future<void> run() async {
// [argResults] is set before [run()] is called and contains the flags/options
// passed to this command.
if (argResults!.rest.length != 1) {
print(
"Expected 1 positional arguments, found ${argResults!.rest.length}",
);
}
var fileName = argResults!.rest[0];
print("Parsing file: $fileName");
var list = await parseInputFile(fileName);
int dialValue = 50;
int combination = 0;
for (var instruction in list) {
switch (instruction.direction) {
case Direction.left:
// Dial counter-clockwise by amount
dialValue -= instruction.amount;
case Direction.right:
// Dial clockwise by amount
dialValue += instruction.amount;
}
dialValue = dialValue % 100;
if (dialValue == 0) {
combination++;
}
}
print("Combination: $combination");
}
}