Day 13: Point of Incidence

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)
  • Code block support is not fully rolled out yet but likely will be in the middle of the event. Try to share solutions as both code blocks and using something such as https://topaz.github.io/paste/ , pastebin, or github (code blocks to future proof it for when 0.19 comes out and since code blocks currently function in some apps and some instances as well if they are running a 0.19 beta)

FAQ


🔒 Thread is locked until there’s at least 100 2 star entries on the global leaderboard

🔓 Unlocked

  • Deebster
    link
    fedilink
    arrow-up
    3
    ·
    edit-2
    1 year ago

    I decided to use string comparison for some reason, which meant part two wasn’t as quick as it would have been.

    use std::{cmp, fs, iter};
    
    fn transpose(rows: &[&str]) -> Vec {
        (0..rows[0].len())
            .map(|i| {
                let bytes: Vec<_> = (0..rows.len()).map(|j| rows[j].as_bytes()[i]).collect();
                String::from_utf8(bytes).unwrap()
            })
            .collect()
    }
    
    fn reflection_value(rows: &[&str]) -> Option {
        'row_loop: for i in 0..(rows.len() - 1) {
            if rows[i] != rows[i + 1] {
                continue;
            }
            // we have an initial match
            let other_matches = cmp::min(i, rows.len() - 2 - i);
            for j in 1..=other_matches {
                if rows[i - j] != rows[i + 1 + j] {
                    continue 'row_loop;
                }
            }
            return Some(i as u32 + 1);
        }
        None
    }
    
    fn summary(file_path: &str) -> u32 {
        fs::read_to_string(file_path)
            .expect("Can't read input file")
            .split("\n\n")
            .map(|s| {
                let rows: Vec<&str> = s.split('\n').collect();
    
                if let Some(v) = reflection_value(&rows) {
                    v * 100
                } else {
                    let cols_owned: Vec = transpose(&rows);
                    let cols: Vec<&str> = cols_owned.iter().map(|s| s.as_str()).collect();
                    reflection_value(&cols).expect("No reflections found")
                }
            })
            .sum()
    }
    
    fn str_diff(str1: &str, str2: &str) -> u32 {
        iter::zip(str1.chars(), str2.chars())
            .map(|(s1, s2)| if s1 == s2 { 0 } else { 1 })
            .sum()
    }
    
    fn smudged_reflection_value(rows: &[&str]) -> Option {
        for i in 0..(rows.len() - 1) {
            let num_cmps = cmp::min(i, rows.len() - 2 - i);
            let errs: u32 = (0..=num_cmps)
                .map(|j| str_diff(rows[i - j], rows[i + 1 + j]))
                .sum();
    
            if errs != 1 {
                continue;
            }
            return Some(i as u32 + 1);
        }
        None
    }
    
    fn smudged_summary(file_path: &str) -> u32 {
        fs::read_to_string(file_path)
            .expect("Can't read input file")
            .split("\n\n")
            .map(|s| {
                let rows: Vec<&str> = s.split('\n').collect();
    
                if let Some(v) = smudged_reflection_value(&rows) {
                    v * 100
                } else {
                    let cols_owned: Vec = transpose(&rows);
                    let cols: Vec<&str> = cols_owned.iter().map(|s| s.as_str()).collect();
                    smudged_reflection_value(&cols).expect("No reflections found")
                }
            })
            .sum()
    }
    
    fn main() {
        println!(" normal: {}", summary("d13/input.txt"));
        println!("smudged: {}", smudged_summary("d13/input.txt"));
    }