Day 1 part 1 complete
This commit is contained in:
@@ -3,6 +3,37 @@ import "dart:io";
|
|||||||
|
|
||||||
import "package:args/command_runner.dart";
|
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 {
|
class Day1Command extends Command {
|
||||||
// The [name] and [description] properties must be defined by every
|
// The [name] and [description] properties must be defined by every
|
||||||
// subclass.
|
// subclass.
|
||||||
@@ -19,10 +50,9 @@ class Day1Command extends Command {
|
|||||||
|
|
||||||
// [run] may also return a Future.
|
// [run] may also return a Future.
|
||||||
@override
|
@override
|
||||||
void run() {
|
Future<void> run() async {
|
||||||
// [argResults] is set before [run()] is called and contains the flags/options
|
// [argResults] is set before [run()] is called and contains the flags/options
|
||||||
// passed to this command.
|
// passed to this command.
|
||||||
print(argResults!.rest);
|
|
||||||
|
|
||||||
if (argResults!.rest.length != 1) {
|
if (argResults!.rest.length != 1) {
|
||||||
print(
|
print(
|
||||||
@@ -31,22 +61,29 @@ class Day1Command extends Command {
|
|||||||
}
|
}
|
||||||
var fileName = argResults!.rest[0];
|
var fileName = argResults!.rest[0];
|
||||||
|
|
||||||
var inputFile = File(fileName);
|
print("Parsing file: $fileName");
|
||||||
var openFile = inputFile.openRead();
|
|
||||||
|
|
||||||
openFile
|
var list = await parseInputFile(fileName);
|
||||||
.transform(utf8.decoder)
|
|
||||||
.transform(LineSplitter())
|
int dialValue = 50;
|
||||||
.listen(
|
int combination = 0;
|
||||||
(String line) {
|
|
||||||
print('$line: ${line.length} bytes');
|
for (var instruction in list) {
|
||||||
},
|
switch (instruction.direction) {
|
||||||
onDone: () {
|
case Direction.left:
|
||||||
print('File is now closed.');
|
// Dial counter-clockwise by amount
|
||||||
},
|
dialValue -= instruction.amount;
|
||||||
onError: (e) {
|
case Direction.right:
|
||||||
print(e.toString());
|
// Dial clockwise by amount
|
||||||
},
|
dialValue += instruction.amount;
|
||||||
);
|
}
|
||||||
|
|
||||||
|
dialValue = dialValue % 100;
|
||||||
|
if (dialValue == 0) {
|
||||||
|
combination++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
print("Combination: $combination");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user