89 lines
2.2 KiB
Dart
89 lines
2.2 KiB
Dart
import "dart:convert";
|
|
import "dart:io";
|
|
|
|
import "package:args/command_runner.dart";
|
|
|
|
List getAdjacent(List<List<bool>> map, int idx, int idy) {
|
|
var adjacentValues = [];
|
|
for (int x in [-1, 0, 1]) {
|
|
for (int y in [-1, 0, 1]) {
|
|
if (x == 0 && y == 0) {
|
|
continue;
|
|
}
|
|
var xSlewed = idx + x;
|
|
var ySlewed = idy + y;
|
|
//print("$xSlewed, $ySlewed");
|
|
try {
|
|
//print(map[ySlewed][xSlewed]);
|
|
adjacentValues.insert(adjacentValues.length, map[ySlewed][xSlewed]);
|
|
} on RangeError {
|
|
continue;
|
|
}
|
|
}
|
|
}
|
|
return adjacentValues;
|
|
}
|
|
|
|
class Day4Command extends Command {
|
|
// The [name] and [description] properties must be defined by every
|
|
// subclass.
|
|
@override
|
|
final name = "day4";
|
|
|
|
@override
|
|
final description = "Run Advent of Code 2025 Day 3";
|
|
|
|
Day4Command() {
|
|
// we can add command specific arguments here.
|
|
// [argParser] is automatically created by the parent class.
|
|
}
|
|
|
|
// [run] may also return a Future.
|
|
@override
|
|
Future<void> run() async {
|
|
// [argResults] is set before [run()] is called and contains the flags/options
|
|
// passed to this command.
|
|
|
|
if (argResults!.rest.length != 1) {
|
|
print(
|
|
"Expected 1 positional arguments, found ${argResults!.rest.length}",
|
|
);
|
|
exit(1);
|
|
}
|
|
var filePath = argResults!.rest[0];
|
|
|
|
print("Parsing file: $filePath");
|
|
var inputFile = File(filePath);
|
|
var paperRollMap = await inputFile
|
|
.openRead()
|
|
.transform(utf8.decoder)
|
|
.transform(LineSplitter())
|
|
.map((line) {
|
|
return line
|
|
.split('')
|
|
.map((ch) => (ch == "@") ? true : false)
|
|
.toList(growable: false);
|
|
})
|
|
.toList();
|
|
//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++;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
print("Part 1 Result: $accumulator");
|
|
}
|
|
}
|