Complete Day 3 Part 2
This commit is contained in:
@@ -37,6 +37,7 @@ class Day3Command extends Command {
|
||||
.openRead()
|
||||
.transform(utf8.decoder)
|
||||
.transform(LineSplitter())
|
||||
.asBroadcastStream()
|
||||
.map((line) {
|
||||
int tens = -1;
|
||||
int ones = -1;
|
||||
@@ -57,5 +58,47 @@ class Day3Command extends Command {
|
||||
.reduce((left, right) => left + right);
|
||||
|
||||
print("Part 1 Answer: $part1Result");
|
||||
|
||||
var part2Result = await inputFile
|
||||
.openRead()
|
||||
.transform(utf8.decoder)
|
||||
.transform(LineSplitter())
|
||||
.asBroadcastStream()
|
||||
.map((line) {
|
||||
int resultLength = 12;
|
||||
var resultList = List.generate(
|
||||
resultLength,
|
||||
(i) => 0,
|
||||
growable: false,
|
||||
);
|
||||
final chars = line.split("");
|
||||
|
||||
for (final (charIdx, char) in chars.indexed) {
|
||||
final charAsInt = int.parse(char);
|
||||
var charsLeftInStream = chars.length - charIdx - 1;
|
||||
for (final (placeIdx, placeValue) in resultList.indexed) {
|
||||
var placesLeft = resultList.length - placeIdx - 1;
|
||||
if (placesLeft > charsLeftInStream) {
|
||||
continue;
|
||||
}
|
||||
if (charAsInt > placeValue) {
|
||||
resultList[placeIdx] = charAsInt;
|
||||
resultList.fillRange(placeIdx + 1, resultList.length, 0);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int result = 0;
|
||||
for (final placeValue in resultList) {
|
||||
result *= 10;
|
||||
result += placeValue;
|
||||
}
|
||||
|
||||
return result;
|
||||
})
|
||||
.reduce((left, right) => left + right);
|
||||
|
||||
print("Part 2 Answer: $part2Result");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user