Day 4: Printing Department

Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL

FAQ

you are viewing a single comment's thread
view the rest of the comments
[โ€“] 1 point 10 months ago

Rust

View on github

fn parse_input(input: &str) -> Vec<Vec<bool>> {
    input
        .lines()
        .map(|l| l.chars().map(|c| c == '@').collect())
        .collect()
}

fn count_adj(grid: &[Vec<bool>], (x, y): (usize, usize)) -> usize {
    let width = grid[0].len();
    let height = grid.len();
    grid.iter()
        .take((y + 2).min(height))
        .skip(y.saturating_sub(1))
        .map(|r| {
            r.iter()
                .take((x + 2).min(width))
                .skip(x.saturating_sub(1))
                .take(3)
                .filter(|e| **e)
                .count()
        })
        .sum::<usize>()
}

fn part1(input: String) {
    let grid = parse_input(&input);
    let mut count = 0u32;
    for (y, row) in grid.iter().enumerate() {
        for (x, _) in row.iter().enumerate().filter(|(_, r)| **r) {
            let n_adj = count_adj(&grid, (x, y));
            // Center roll is counted too
            if n_adj < 5 {
                count += 1;
            }
        }
    }
    println!("{count}");
}

fn part2(input: String) {
    let mut grid = parse_input(&input);
    let mut removed = 0u32;
    loop {
        let mut next_grid = grid.clone();
        let prev_removed = removed;
        for (y, row) in grid.iter().enumerate() {
            for (x, _) in row.iter().enumerate().filter(|(_, r)| **r) {
                let n_adj = count_adj(&grid, (x, y));
                // Center roll is counted too
                if n_adj < 5 {
                    next_grid[y][x] = false;
                    removed += 1;
                }
            }
        }
        if removed == prev_removed {
            break;
        }
        grid = next_grid;
    }
    println!("{}", removed);
}

util::aoc_main!();
  • source