diff --git a/lib/day4/command.dart b/lib/day4/command.dart index 2a8bdd0..fdc5e70 100644 --- a/lib/day4/command.dart +++ b/lib/day4/command.dart @@ -15,7 +15,7 @@ List getAdjacent(List> 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> map, int idx, int idy) { return adjacentValues; } +(int, List>) removeRolls(List> rollMap) { + int rollsRemoved = 0; + List> newRollMap = List.from( + rollMap.map((e) => List.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"); } }