Skip to content
Snippets Groups Projects

AoC 2023 day 13 part 1

  • Clone with SSH
  • Clone with HTTPS
  • Embed
  • Share
    The snippet can be accessed without any authentication.
    Authored by s91149
    main.rs 1.22 KiB
    fn main() {
        let input = std::fs::read_to_string("../inputs/input.txt").unwrap();
    
        let mut above_sum = 0;
        let mut left_sum = 0;
    
        for block in input.split("\n\n") {
            let block: Vec<Vec<char>> = block.lines().map(|line| line.chars().collect()).collect();
            above_sum += count_above_reflect_point(&block);
            let transposed = transpose(&block);
            left_sum += count_above_reflect_point(&transposed);
        }
    
        println!("part 1: {}", left_sum + 100 * above_sum);
    }
    
    fn transpose(block: &[Vec<char>]) -> Vec<Vec<char>> {
        let mut transposed: Vec<Vec<char>> = Vec::new();
        let block_height = block.len();
        let block_width = block[1].len();
    
        for x in 0..block_width {
            let mut v = Vec::with_capacity(block_height);
            for y in 0..block_height {
                v.push(block[y][x]);
            }
            transposed.push(v);
        }
        transposed
    }
    
    fn count_above_reflect_point(block: &[Vec<char>]) -> u32 {
        for i in 1..block.len() {
            if block[i] == block[i - 1] {
                let before = &block[..i];
                let after = &block[i..];
                if before.iter().rev().zip(after.iter()).all(|(a, b)| a == b) {
                    return i as u32;
                }
            }
        }
        0
    }
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Please register or to comment