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