Complete Day 4

This commit is contained in:
2025-12-09 15:09:42 -06:00
parent 016f8405e7
commit 08ac0266fc

View File

@@ -15,7 +15,7 @@ List getAdjacent(List<List<bool>> map, int idx, int idy) {
//print("$xSlewed, $ySlewed");
try {
//print(map[ySlewed][xSlewed]);
adjacentValues.insert(adjacentValues.length, map[ySlewed][xSlewed]);
adjacentValues.add(map[ySlewed][xSlewed]);
} on RangeError {
continue;
}
@@ -24,6 +24,30 @@ List getAdjacent(List<List<bool>> map, int idx, int idy) {
return adjacentValues;
}
(int, List<List<bool>>) removeRolls(List<List<bool>> rollMap) {
int rollsRemoved = 0;
List<List<bool>> newRollMap = List.from(
rollMap.map((e) => List<bool>.from(e, growable: false)),
growable: false,
);
for (var (y, row) in rollMap.indexed) {
for (var (x, columnValue) in row.indexed) {
if (columnValue == true) {
var numAdjacent = getAdjacent(
rollMap,
x,
y,
).where((value) => value).length;
if (numAdjacent < 4) {
rollsRemoved++;
newRollMap[y][x] = false;
}
}
}
}
return (rollsRemoved, newRollMap);
}
class Day4Command extends Command {
// The [name] and [description] properties must be defined by every
// subclass.
@@ -68,21 +92,18 @@ class Day4Command extends Command {
//print(paperRollMap);
//print(getAdjacent(paperRollMap, 0, 0));
int accumulator = 0;
for (var (y, row) in paperRollMap.indexed) {
for (var (x, columnValue) in row.indexed) {
if (columnValue == true) {
var numAdjacent = getAdjacent(
paperRollMap,
x,
y,
).where((value) => value).length;
if (numAdjacent < 4) {
accumulator++;
}
}
int round = 1;
int totalRollsRemoved = 0;
while (true) {
int rollsRemoved;
(rollsRemoved, paperRollMap) = removeRolls(paperRollMap);
print("Rolls Removed During Round $round: $rollsRemoved");
if (rollsRemoved == 0) {
break;
}
totalRollsRemoved += rollsRemoved;
round++;
}
print("Part 1 Result: $accumulator");
print("Total Rolls Removed: $totalRollsRemoved");
}
}