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)
}
}
}