Complete day 4 part 1
This commit is contained in:
194
lib/day4/README.txt
Normal file
194
lib/day4/README.txt
Normal file
@@ -0,0 +1,194 @@
|
||||
|
||||
Advent of Code
|
||||
|
||||
[About][Events][Shop][Settings][Log Out]
|
||||
|
||||
Bearmine 7*
|
||||
$year=2025;
|
||||
|
||||
[Calendar][AoC++][Sponsors][Leaderboards][Stats]
|
||||
|
||||
Our sponsors help make Advent of Code possible:
|
||||
Depot - Happy coding and merry builds from Depot. We keep your pipelines fast, your caches warm, and your logs full of cheer, because the holidays are for shipping joy, not waiting on CI. Ship up to 55x faster with Depot.
|
||||
--- Day 4: Printing Department ---
|
||||
|
||||
You ride the escalator down to the printing department. They're clearly getting ready for Christmas; they have lots of large rolls of paper everywhere, and there's even a massive printer in the corner (to handle the really big print jobs).
|
||||
|
||||
Decorating here will be easy: they can make their own decorations. What you really need is a way to get further into the North Pole base while the elevators are offline.
|
||||
|
||||
"Actually, maybe we can help with that," one of the Elves replies when you ask for help. "We're pretty sure there's a cafeteria on the other side of the back wall. If we could break through the wall, you'd be able to keep moving. It's too bad all of our forklifts are so busy moving those big rolls of paper around."
|
||||
|
||||
If you can optimize the work the forklifts are doing, maybe they would have time to spare to break through the wall.
|
||||
|
||||
The rolls of paper (@) are arranged on a large grid; the Elves even have a helpful diagram (your puzzle input) indicating where everything is located.
|
||||
|
||||
For example:
|
||||
|
||||
..@@.@@@@.
|
||||
@@@.@.@.@@
|
||||
@@@@@.@.@@
|
||||
@.@@@@..@.
|
||||
@@.@@@@.@@
|
||||
.@@@@@@@.@
|
||||
.@.@.@.@@@
|
||||
@.@@@.@@@@
|
||||
.@@@@@@@@.
|
||||
@.@.@@@.@.
|
||||
|
||||
The forklifts can only access a roll of paper if there are fewer than four rolls of paper in the eight adjacent positions. If you can figure out which rolls of paper the forklifts can access, they'll spend less time looking and more time breaking down the wall to the cafeteria.
|
||||
|
||||
In this example, there are 13 rolls of paper that can be accessed by a forklift (marked with x):
|
||||
|
||||
..xx.xx@x.
|
||||
x@@.@.@.@@
|
||||
@@@@@.x.@@
|
||||
@.@@@@..@.
|
||||
x@.@@@@.@x
|
||||
.@@@@@@@.@
|
||||
.@.@.@.@@@
|
||||
x.@@@.@@@@
|
||||
.@@@@@@@@.
|
||||
x.x.@@@.x.
|
||||
|
||||
Consider your complete diagram of the paper roll locations. How many rolls of paper can be accessed by a forklift?
|
||||
|
||||
Your puzzle answer was 1441.
|
||||
|
||||
The first half of this puzzle is complete! It provides one gold star: *
|
||||
--- Part Two ---
|
||||
|
||||
Now, the Elves just need help accessing as much of the paper as they can.
|
||||
|
||||
Once a roll of paper can be accessed by a forklift, it can be removed. Once a roll of paper is removed, the forklifts might be able to access more rolls of paper, which they might also be able to remove. How many total rolls of paper could the Elves remove if they keep repeating this process?
|
||||
|
||||
Starting with the same example as above, here is one way you could remove as many rolls of paper as possible, using highlighted @ to indicate that a roll of paper is about to be removed, and using x to indicate that a roll of paper was just removed:
|
||||
|
||||
Initial state:
|
||||
..@@.@@@@.
|
||||
@@@.@.@.@@
|
||||
@@@@@.@.@@
|
||||
@.@@@@..@.
|
||||
@@.@@@@.@@
|
||||
.@@@@@@@.@
|
||||
.@.@.@.@@@
|
||||
@.@@@.@@@@
|
||||
.@@@@@@@@.
|
||||
@.@.@@@.@.
|
||||
|
||||
Remove 13 rolls of paper:
|
||||
..xx.xx@x.
|
||||
x@@.@.@.@@
|
||||
@@@@@.x.@@
|
||||
@.@@@@..@.
|
||||
x@.@@@@.@x
|
||||
.@@@@@@@.@
|
||||
.@.@.@.@@@
|
||||
x.@@@.@@@@
|
||||
.@@@@@@@@.
|
||||
x.x.@@@.x.
|
||||
|
||||
Remove 12 rolls of paper:
|
||||
.......x..
|
||||
.@@.x.x.@x
|
||||
x@@@@...@@
|
||||
x.@@@@..x.
|
||||
.@.@@@@.x.
|
||||
.x@@@@@@.x
|
||||
.x.@.@.@@@
|
||||
..@@@.@@@@
|
||||
.x@@@@@@@.
|
||||
....@@@...
|
||||
|
||||
Remove 7 rolls of paper:
|
||||
..........
|
||||
.x@.....x.
|
||||
.@@@@...xx
|
||||
..@@@@....
|
||||
.x.@@@@...
|
||||
..@@@@@@..
|
||||
...@.@.@@x
|
||||
..@@@.@@@@
|
||||
..x@@@@@@.
|
||||
....@@@...
|
||||
|
||||
Remove 5 rolls of paper:
|
||||
..........
|
||||
..x.......
|
||||
.x@@@.....
|
||||
..@@@@....
|
||||
...@@@@...
|
||||
..x@@@@@..
|
||||
...@.@.@@.
|
||||
..x@@.@@@x
|
||||
...@@@@@@.
|
||||
....@@@...
|
||||
|
||||
Remove 2 rolls of paper:
|
||||
..........
|
||||
..........
|
||||
..x@@.....
|
||||
..@@@@....
|
||||
...@@@@...
|
||||
...@@@@@..
|
||||
...@.@.@@.
|
||||
...@@.@@@.
|
||||
...@@@@@x.
|
||||
....@@@...
|
||||
|
||||
Remove 1 roll of paper:
|
||||
..........
|
||||
..........
|
||||
...@@.....
|
||||
..x@@@....
|
||||
...@@@@...
|
||||
...@@@@@..
|
||||
...@.@.@@.
|
||||
...@@.@@@.
|
||||
...@@@@@..
|
||||
....@@@...
|
||||
|
||||
Remove 1 roll of paper:
|
||||
..........
|
||||
..........
|
||||
...x@.....
|
||||
...@@@....
|
||||
...@@@@...
|
||||
...@@@@@..
|
||||
...@.@.@@.
|
||||
...@@.@@@.
|
||||
...@@@@@..
|
||||
....@@@...
|
||||
|
||||
Remove 1 roll of paper:
|
||||
..........
|
||||
..........
|
||||
....x.....
|
||||
...@@@....
|
||||
...@@@@...
|
||||
...@@@@@..
|
||||
...@.@.@@.
|
||||
...@@.@@@.
|
||||
...@@@@@..
|
||||
....@@@...
|
||||
|
||||
Remove 1 roll of paper:
|
||||
..........
|
||||
..........
|
||||
..........
|
||||
...x@@....
|
||||
...@@@@...
|
||||
...@@@@@..
|
||||
...@.@.@@.
|
||||
...@@.@@@.
|
||||
...@@@@@..
|
||||
....@@@...
|
||||
|
||||
Stop once no more rolls of paper are accessible by a forklift. In this example, a total of 43 rolls of paper can be removed.
|
||||
|
||||
Start with your original diagram. How many rolls of paper in total can be removed by the Elves and their forklifts?
|
||||
|
||||
Answer:
|
||||
|
||||
Although it hasn't changed, you can still get your puzzle input.
|
||||
|
||||
You can also [Shareon Bluesky Twitter Mastodon] this puzzle.
|
||||
@@ -3,6 +3,27 @@ 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.
|
||||
@@ -32,5 +53,36 @@ class Day4Command extends Command {
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user