Day 6: Trash Compactor

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

Mainly difficult parsing today.

View on github

fn part1(input: String) {
    let mut nums: Vec<Vec<u64>> = Vec::new();
    let mut mul: Vec<bool> = Vec::new();
    for l in input.lines() {
        if l.chars().next().unwrap().is_ascii_digit() {
            let row = l
                .split_ascii_whitespace()
                .map(|s| s.parse::<u64>().unwrap())
                .collect();
            nums.push(row);
        } else {
            mul = l.split_ascii_whitespace().map(|s| s == "*").collect();
        }
    }
    let mut sum = 0;
    for (idx, op_mul) in mul.iter().enumerate() {
        let col = nums.iter().map(|row| row[idx]);
        sum += if *op_mul {
            col.reduce(|acc, n| acc * n)
        } else {
            col.reduce(|acc, n| acc + n)
        }
        .unwrap();
    }
    println!("{sum}");
}

fn part2(input: String) {
    let grid: Vec<&[u8]> = input.lines().map(|l| l.as_bytes()).collect();
    let n_rows = grid.len() - 1; // Not counting operator row
    let mut op_mul = grid[n_rows][0] == b'*';
    let mut cur = if op_mul { 1 } else { 0 };
    let mut sum = 0;
    for x in 0..grid[0].len() {
        let digits: Vec<u8> = (0..n_rows).map(|y| grid[y][x]).collect();
        if digits.iter().all(|d| *d == b' ') {
            sum += cur;
            op_mul = grid[n_rows][x + 1] == b'*';
            cur = if op_mul { 1 } else { 0 };
            continue;
        }
        let n = String::from_utf8(digits)
            .unwrap()
            .trim()
            .parse::<u64>()
            .unwrap();
        if op_mul {
            cur *= n;
        } else {
            cur += n;
        }
    }
    sum += cur;
    println!("{sum}");
}

util::aoc_main!();
  • source