Agenda and Timeline
- 18:30 - 19:00: Welcome, Grab a drink, Grab a Pizza, Set up your workspace
- 19:00 - 19:30: Introduction to the Hack Evening and the Challenge
- 19:30 - 21:30: Write Code, Ask Questions, Get Help, Repeat - Have Fun!
Introduction
— 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?
Problem Summary
Problem Summary
Input: A grid where @ = paper roll, . = empty
Task: Count paper rolls that have fewer than 4 neighboring paper rolls (8-directional adjacency)
Iteration Walkthrough
Grid (with coordinates):
0123456789
┌──────────
0 │ ..@@.@@@@.
1 │ @@@.@.@.@@
2 │ @@@@@.@.@@
...
Step 1: (0, 0) → .
[.] . @ @ ← current cell marked with [ ]
@ @ @ .
@ @ @ @
Not a @, skip.
Step 2: (0, 1) → .
. [.] @ @
@ @ @ .
@ @ @ @
Not a @, skip.
Step 3: (0, 2) → @ ✓
. . [@] @ Neighbors (5 in bounds, 3 out-of-bounds):
@ @ @ . ┌─────────────┐
@ @ @ @ │ x x x │ x = out of bounds
│ . ░ @ │ ░ = center (not counted)
│ @ @ . │
└─────────────┘
Neighbor @ count: 3 → < 4, vaild! ✓
Step 4: (0, 3) → @ ✓
. . @ [@] Neighbors (3 in bounds):
@ @ @ . ┌─────────────┐
@ @ @ @ │ x x x │
│ @ ░ x │
│ @ . x │
└─────────────┘
Neighbor @ count: 2 → < 4, vaild! ✓
Step 6: (1, 1) → @ ✓
. . @ @ Neighbors (8 in bounds):
@ [@] @ . ┌─────────────┐
@ @ @ @ │ . . @ │
│ @ ░ @ │
│ @ @ @ │
└─────────────┘
Neighbor @ count: 6 → < 4, invalid!
and so on…
Approaces
Data loading
The input.txt data is available for no AoC account holders.
- std lib io api with
File::openandBufReader::new(file).lines() include_str!macro to include the file as a&'static str(docs)include_bytes!macro to include the file as a&'static [u8](docs)
Grid representation
Vec<Vec<Cell>>whereCellis an enum representing empty or paper roll or a simplecharHashSet<(usize, usize)>storing coordinates of paper rolls only- Flat
Vec<Cell>with width and height stored separately for index calculations
Neighbor calculation
- Hardcoded offsets for the 8 directions
- Using
itertools::iproduct!macro to generate offsets - Custom iterator that yields neighbor coordinates
- Optional: async iterator for neighbor calculation using
async-streamcrate
Iterators
- Using standard iterator methods like
filter,map,flat_map, andcollect - Implementing a custom iterator
#![allow(unused)]
fn main() {
// iterator trait:
trait Iterator {
type Item;
fn next(&mut self) -> Option<Self::Item>;
}
}
- Example: Fibonacci iterator
#![allow(unused)]
fn main() {
struct Fibonacci {
curr: u64,
next: u64,
}
impl Fibonacci {
fn new() -> Self {
Fibonacci { curr: 0, next: 1 }
}
}
impl Iterator for Fibonacci {
type Item = u64;
fn next(&mut self) -> Option<Self::Item> {
let new_next = self.curr + self.next;
let curr = self.curr;
self.curr = self.next;
self.next = new_next;
Some(curr)
}
}
}
We need your Feedback!
Help us to improve the Rust Munich Meetup by providing your feedback. Scan the QR code or click the link below to fill out the feedback form.
