From 05311bafe005f50b64bfdc157bdeacd914aff75e Mon Sep 17 00:00:00 2001 From: Bearmine Date: Mon, 1 Dec 2025 16:51:50 -0600 Subject: [PATCH] Day 1 part 1 complete --- lib/day1/command.dart | 73 ++++++++++++++++++++++++++++++++----------- 1 file changed, 55 insertions(+), 18 deletions(-) diff --git a/lib/day1/command.dart b/lib/day1/command.dart index c649310..71f1082 100644 --- a/lib/day1/command.dart +++ b/lib/day1/command.dart @@ -3,6 +3,37 @@ 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> parseInputFile(String filePath) async { + var inputFile = File(filePath); + var openFile = inputFile.openRead(); + + var lines = openFile.transform(utf8.decoder).transform(LineSplitter()); + + List 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. @@ -19,10 +50,9 @@ class Day1Command extends Command { // [run] may also return a Future. @override - void run() { + Future run() async { // [argResults] is set before [run()] is called and contains the flags/options // passed to this command. - print(argResults!.rest); if (argResults!.rest.length != 1) { print( @@ -31,22 +61,29 @@ class Day1Command extends Command { } var fileName = argResults!.rest[0]; - var inputFile = File(fileName); - var openFile = inputFile.openRead(); + print("Parsing file: $fileName"); - openFile - .transform(utf8.decoder) - .transform(LineSplitter()) - .listen( - (String line) { - print('$line: ${line.length} bytes'); - }, - onDone: () { - print('File is now closed.'); - }, - onError: (e) { - print(e.toString()); - }, - ); + 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"); } }