day13: problem 1 solution
This commit is contained in:
		
							
								
								
									
										7
									
								
								13/Cargo.lock
									
									
									
										generated
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								13/Cargo.lock
									
									
									
										generated
									
									
									
										Normal file
									
								
							@@ -0,0 +1,7 @@
 | 
			
		||||
# This file is automatically @generated by Cargo.
 | 
			
		||||
# It is not intended for manual editing.
 | 
			
		||||
version = 3
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "day13"
 | 
			
		||||
version = "0.1.0"
 | 
			
		||||
							
								
								
									
										8
									
								
								13/Cargo.toml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								13/Cargo.toml
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,8 @@
 | 
			
		||||
[package]
 | 
			
		||||
name = "day13"
 | 
			
		||||
version = "0.1.0"
 | 
			
		||||
edition = "2021"
 | 
			
		||||
 | 
			
		||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
 | 
			
		||||
 | 
			
		||||
[dependencies]
 | 
			
		||||
							
								
								
									
										183
									
								
								13/src/main.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										183
									
								
								13/src/main.rs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,183 @@
 | 
			
		||||
use std::fs::File;
 | 
			
		||||
use std::io::{BufRead, BufReader, Lines};
 | 
			
		||||
use std::ops::Index;
 | 
			
		||||
use std::time::Instant;
 | 
			
		||||
 | 
			
		||||
// BOILERPLATE
 | 
			
		||||
type InputIter = Lines<BufReader<File>>;
 | 
			
		||||
 | 
			
		||||
fn get_input() -> InputIter {
 | 
			
		||||
    let f = File::open("input").unwrap();
 | 
			
		||||
    let br = BufReader::new(f);
 | 
			
		||||
    br.lines()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn main() {
 | 
			
		||||
    let start = Instant::now();
 | 
			
		||||
    let ans1 = problem1(get_input());
 | 
			
		||||
    let duration = start.elapsed();
 | 
			
		||||
    println!("Problem 1 solution: {} [{}s]", ans1, duration.as_secs_f64());
 | 
			
		||||
 | 
			
		||||
    let start = Instant::now();
 | 
			
		||||
    let ans2 = problem2(get_input());
 | 
			
		||||
    let duration = start.elapsed();
 | 
			
		||||
    println!("Problem 2 solution: {} [{}s]", ans2, duration.as_secs_f64());
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// PARSE
 | 
			
		||||
 | 
			
		||||
#[derive(Debug)]
 | 
			
		||||
struct Pattern {
 | 
			
		||||
    mirrors: Vec<Vec<char>>,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Pattern {
 | 
			
		||||
    fn width(&self) -> usize {
 | 
			
		||||
        self.mirrors[0].len()
 | 
			
		||||
    }
 | 
			
		||||
    fn height(&self) -> usize {
 | 
			
		||||
        self.mirrors.len()
 | 
			
		||||
    }
 | 
			
		||||
    fn col(&self, idx: usize) -> Vec<char> {
 | 
			
		||||
        self.mirrors.iter().map(|row| row[idx]).collect()
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn find_horizontal_reflection(&self) -> Option<usize> {
 | 
			
		||||
        println!("looking for horiz: {:?}", (1..self.height() - 1).collect::<Vec<_>>());
 | 
			
		||||
        (1..self.height()).find(|idx| self.is_horizontal_reflection(*idx))
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn is_horizontal_reflection(&self, idx: usize) -> bool {
 | 
			
		||||
        let row_pairs = std::cmp::min(idx, self.height() - idx);
 | 
			
		||||
        (0..row_pairs).all(|offset| {
 | 
			
		||||
            println!(
 | 
			
		||||
                "{}: [{} == {}] {:?} == {:?}",
 | 
			
		||||
                idx,
 | 
			
		||||
                idx - offset - 1,
 | 
			
		||||
                idx + offset,
 | 
			
		||||
                self.mirrors[idx - offset - 1],
 | 
			
		||||
                self.mirrors[idx + offset]
 | 
			
		||||
            );
 | 
			
		||||
            self.mirrors[idx - offset - 1] == self.mirrors[idx + offset]
 | 
			
		||||
        })
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn find_vertical_reflection(&self) -> Option<usize> {
 | 
			
		||||
        println!("looking for vert: {:?}", (1..self.width() - 1).collect::<Vec<_>>());
 | 
			
		||||
        (1..self.width() ).find(|idx| self.is_vertical_reflection(*idx))
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn is_vertical_reflection(&self, idx: usize) -> bool {
 | 
			
		||||
        let col_pairs = std::cmp::min(idx, self.width() - idx);
 | 
			
		||||
        (0..col_pairs).all(|offset| {
 | 
			
		||||
            println!(
 | 
			
		||||
                "{}: [{} == {}] {:?} == {:?}",
 | 
			
		||||
                idx,
 | 
			
		||||
                idx - offset - 1,
 | 
			
		||||
                idx + offset,
 | 
			
		||||
                self.col(idx - offset - 1),
 | 
			
		||||
                self.col(idx + offset)
 | 
			
		||||
            );
 | 
			
		||||
            self.col(idx - offset - 1) == self.col(idx + offset)
 | 
			
		||||
        })
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn reflection_cost(&self) -> u64 {
 | 
			
		||||
        if let Some(refl) = self.find_horizontal_reflection() {
 | 
			
		||||
            println!("horizontal found at {}", refl);
 | 
			
		||||
            100 * refl as u64
 | 
			
		||||
        } else if let Some(refl) = self.find_vertical_reflection() {
 | 
			
		||||
            println!("vertical found at {}", refl);
 | 
			
		||||
            refl as u64
 | 
			
		||||
        } else {
 | 
			
		||||
            panic!("no reflection");
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(Debug)]
 | 
			
		||||
struct Patterns {
 | 
			
		||||
    patterns: Vec<Pattern>,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl<T: BufRead> From<Lines<T>> for Patterns {
 | 
			
		||||
    fn from(mut lines: Lines<T>) -> Self {
 | 
			
		||||
        let mut patterns = Vec::new();
 | 
			
		||||
        let mut cur_lines = Vec::new();
 | 
			
		||||
        while let Some(Ok(line)) = lines.next() {
 | 
			
		||||
            if line.len() == 0 {
 | 
			
		||||
                patterns.push(Pattern { mirrors: cur_lines });
 | 
			
		||||
                cur_lines = Vec::new();
 | 
			
		||||
            } else {
 | 
			
		||||
                cur_lines.push(line.chars().collect());
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        patterns.push(Pattern { mirrors: cur_lines });
 | 
			
		||||
        Self { patterns }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// PROBLEM 1 solution
 | 
			
		||||
 | 
			
		||||
fn problem1<T: BufRead>(input: Lines<T>) -> u64 {
 | 
			
		||||
    let patterns = Patterns::from(input);
 | 
			
		||||
 | 
			
		||||
    patterns
 | 
			
		||||
        .patterns
 | 
			
		||||
        .iter()
 | 
			
		||||
        .map(|pat| { println!("{:?}", pat); pat.reflection_cost() })
 | 
			
		||||
        .sum()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// PROBLEM 2 solution
 | 
			
		||||
fn problem2<T: BufRead>(input: Lines<T>) -> u64 {
 | 
			
		||||
    0
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[cfg(test)]
 | 
			
		||||
mod tests {
 | 
			
		||||
    use crate::*;
 | 
			
		||||
    use std::io::Cursor;
 | 
			
		||||
 | 
			
		||||
    const EXAMPLE: &str = &"#.##..##.
 | 
			
		||||
..#.##.#.
 | 
			
		||||
##......#
 | 
			
		||||
##......#
 | 
			
		||||
..#.##.#.
 | 
			
		||||
..##..##.
 | 
			
		||||
#.#.##.#.
 | 
			
		||||
 | 
			
		||||
#...##..#
 | 
			
		||||
#....#..#
 | 
			
		||||
..##..###
 | 
			
		||||
#####.##.
 | 
			
		||||
#####.##.
 | 
			
		||||
..##..###
 | 
			
		||||
#....#..#";
 | 
			
		||||
 | 
			
		||||
    #[test]
 | 
			
		||||
    fn problem1_find_horizontal() {
 | 
			
		||||
        let c = Cursor::new(EXAMPLE);
 | 
			
		||||
        let patterns = Patterns::from(c.lines());
 | 
			
		||||
        assert_eq!(patterns.patterns[1].find_horizontal_reflection(), Some(4));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    #[test]
 | 
			
		||||
    fn problem1_find_vertical() {
 | 
			
		||||
        let c = Cursor::new(EXAMPLE);
 | 
			
		||||
        let patterns = Patterns::from(c.lines());
 | 
			
		||||
        assert_eq!(patterns.patterns[0].find_vertical_reflection(), Some(5));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    #[test]
 | 
			
		||||
    fn problem1_example() {
 | 
			
		||||
        let c = Cursor::new(EXAMPLE);
 | 
			
		||||
        assert_eq!(problem1(c.lines()), 405);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    #[test]
 | 
			
		||||
    fn problem2_example() {
 | 
			
		||||
        let c = Cursor::new(EXAMPLE);
 | 
			
		||||
        assert_eq!(problem2(c.lines()), 0);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user