day14: problem 1 solution
This commit is contained in:
		
							
								
								
									
										184
									
								
								14/src/main.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										184
									
								
								14/src/main.rs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,184 @@
 | 
			
		||||
use std::fmt::{Display, Write};
 | 
			
		||||
use std::fs::File;
 | 
			
		||||
use std::io::{BufRead, BufReader, Lines};
 | 
			
		||||
use std::time::{Duration, 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
 | 
			
		||||
 | 
			
		||||
struct Platform(Vec<Vec<char>>);
 | 
			
		||||
 | 
			
		||||
impl<T: BufRead> From<Lines<T>> for Platform {
 | 
			
		||||
    fn from(lines: Lines<T>) -> Self {
 | 
			
		||||
        Self(lines.map(|line| line.unwrap().chars().collect()).collect())
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
enum Direction {
 | 
			
		||||
    North,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(Debug, PartialEq)]
 | 
			
		||||
enum Axis {
 | 
			
		||||
    Rows,
 | 
			
		||||
    Columns,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Direction {
 | 
			
		||||
    fn reverse_direction(&self) -> bool {
 | 
			
		||||
        match self {
 | 
			
		||||
            Direction::North => false,
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    fn axis(&self) -> Axis {
 | 
			
		||||
        match self {
 | 
			
		||||
            Direction::North => Axis::Columns,
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl<'a> Platform {
 | 
			
		||||
    fn width(&self) -> usize {
 | 
			
		||||
        self.0[0].len()
 | 
			
		||||
    }
 | 
			
		||||
    fn col(&self, col: usize) -> Vec<char> {
 | 
			
		||||
        self.0.iter().map(|row| row[col]).collect()
 | 
			
		||||
    }
 | 
			
		||||
    fn replace_col(&mut self, idx: usize, col: Vec<char>) {
 | 
			
		||||
        for row_idx in 0..col.len() {
 | 
			
		||||
            self.0[row_idx][idx] = col[row_idx];
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    fn roll(&mut self, dir: &Direction) {
 | 
			
		||||
        match dir.axis() {
 | 
			
		||||
            Axis::Columns => self.roll_columns(&dir),
 | 
			
		||||
            Axis::Rows => self.roll_rows(&dir),
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    fn score(&self, dir: &Direction) -> u64 {
 | 
			
		||||
        match dir.axis() {
 | 
			
		||||
            Axis::Columns => self.score_columns(dir),
 | 
			
		||||
            Axis::Rows => self.score_rows(dir),
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    fn roll_columns(&mut self, dir: &Direction) {
 | 
			
		||||
        assert_eq!(dir.axis(), Axis::Columns);
 | 
			
		||||
 | 
			
		||||
        for col in 0..self.width() {
 | 
			
		||||
            self.roll_column(col, dir);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    fn roll_column(&mut self, idx: usize, dir: &Direction) {
 | 
			
		||||
        let mut col = self.col(idx);
 | 
			
		||||
        // TODO: implement reverse direction
 | 
			
		||||
        for col_idx in 0..col.len() {
 | 
			
		||||
            match col[col_idx] {
 | 
			
		||||
                '.' | '#' => continue,
 | 
			
		||||
                'O' if col_idx == 0 => continue,
 | 
			
		||||
                'O' => {
 | 
			
		||||
                    // Find the first # rock or the edge of the map, we can't look beyond this for a resting position
 | 
			
		||||
                    let lower_limit = (0..col_idx).rev().find(|c| col[*c] == '#').unwrap_or(0);
 | 
			
		||||
 | 
			
		||||
                    if let Some(empty_pos) = (lower_limit..col_idx)
 | 
			
		||||
                        .rev()
 | 
			
		||||
                        .filter(|i| col[*i] == '.')
 | 
			
		||||
                        .last()
 | 
			
		||||
                    {
 | 
			
		||||
                        col.swap(col_idx, empty_pos);
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
                _ => panic!("invalid character"),
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        self.replace_col(idx, col);
 | 
			
		||||
    }
 | 
			
		||||
    fn score_columns(&self, dir: &Direction) -> u64 {
 | 
			
		||||
        // TODO: implement reverse direction
 | 
			
		||||
        (0..self.0.len())
 | 
			
		||||
            .map(|row| {
 | 
			
		||||
                let row_score = self.0.len() - row;
 | 
			
		||||
                self.0[row].iter().filter(|c| **c == 'O').count() * row_score
 | 
			
		||||
            })
 | 
			
		||||
            .sum::<usize>() as u64
 | 
			
		||||
    }
 | 
			
		||||
    fn roll_rows(&self, dir: &Direction) {
 | 
			
		||||
        unimplemented!()
 | 
			
		||||
    }
 | 
			
		||||
    fn score_rows(&self, dir: &Direction) -> u64 {
 | 
			
		||||
        unimplemented!()
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Display for Platform {
 | 
			
		||||
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
 | 
			
		||||
        for row in &self.0 {
 | 
			
		||||
            for c in row {
 | 
			
		||||
                f.write_char(*c)?;
 | 
			
		||||
            }
 | 
			
		||||
            writeln!(f)?;
 | 
			
		||||
        }
 | 
			
		||||
        writeln!(f)
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// PROBLEM 1 solution
 | 
			
		||||
 | 
			
		||||
fn problem1<T: BufRead>(input: Lines<T>) -> u64 {
 | 
			
		||||
    let mut p = Platform::from(input);
 | 
			
		||||
    p.roll(&Direction::North);
 | 
			
		||||
    p.score(&Direction::North)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 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 = &"O....#....
 | 
			
		||||
O.OO#....#
 | 
			
		||||
.....##...
 | 
			
		||||
OO.#O....O
 | 
			
		||||
.O.....O#.
 | 
			
		||||
O.#..O.#.#
 | 
			
		||||
..O..#O..O
 | 
			
		||||
.......O..
 | 
			
		||||
#....###..
 | 
			
		||||
#OO..#....";
 | 
			
		||||
 | 
			
		||||
    #[test]
 | 
			
		||||
    fn problem1_example() {
 | 
			
		||||
        let c = Cursor::new(EXAMPLE);
 | 
			
		||||
        assert_eq!(problem1(c.lines()), 136);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    #[test]
 | 
			
		||||
    fn problem2_example() {
 | 
			
		||||
        let c = Cursor::new(EXAMPLE);
 | 
			
		||||
        assert_eq!(problem2(c.lines()), 0);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user