Day 2 Part 1 complete
This commit is contained in:
@@ -33,13 +33,31 @@ class Day2Command extends Command {
|
||||
print("Parsing file: $filePath");
|
||||
|
||||
var inputFile = File(filePath);
|
||||
var openFile = inputFile.openRead();
|
||||
|
||||
var lines = openFile.transform(utf8.decoder).transform(LineSplitter());
|
||||
|
||||
await for (var line in lines) {
|
||||
print(line);
|
||||
print('-------');
|
||||
}
|
||||
var openFile = await inputFile.readAsString();
|
||||
var part1Answer = openFile
|
||||
.trim()
|
||||
.split(',')
|
||||
.expand((range) {
|
||||
var extents = range.split('-');
|
||||
if (extents.length != 2) {
|
||||
throw Exception(
|
||||
"Expected range with 2 parts, but got ${extents.length} parts: {range}",
|
||||
);
|
||||
}
|
||||
var numRange = [int.parse(extents[0]), int.parse(extents[1])];
|
||||
List<int> createRange(int from, int to) =>
|
||||
List.generate(to - from + 1, (i) => i + from);
|
||||
return createRange(numRange[0], numRange[1]);
|
||||
})
|
||||
.map((id) => id.toString())
|
||||
.where((id) => id.length.isEven)
|
||||
.where((id) {
|
||||
var first_half = id.substring(0, (id.length / 2).floor());
|
||||
var second_half = id.substring((id.length / 2).floor(), id.length);
|
||||
return first_half == second_half;
|
||||
})
|
||||
.map((id) => int.parse(id))
|
||||
.reduce((id1, id2) => id1 + id2);
|
||||
print("Part 1 Answer: $part1Answer");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user