Day 1 part 1 complete
This commit is contained in:
@@ -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<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.
|
||||
@@ -19,10 +50,9 @@ class Day1Command extends Command {
|
||||
|
||||
// [run] may also return a Future.
|
||||
@override
|
||||
void run() {
|
||||
Future<void> 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");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user