Complete Day 3 Part 2

This commit is contained in:
2025-12-06 17:07:53 -06:00
parent ffdbd1e706
commit 3b6a041a74
2 changed files with 119 additions and 0 deletions

View File

@@ -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");
}
}