Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Approaces

Data loading

The input.txt data is available for no AoC account holders.

  • std lib io api with File::open and BufReader::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>> where Cell is an enum representing empty or paper roll or a simple char
  • HashSet<(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-stream crate

Iterators

  • Using standard iterator methods like filter, map, flat_map, and collect
  • 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)
    }
}
}