solution: day 8
This commit is contained in:
parent
cd4dab86c8
commit
c32121a4b2
@ -5,7 +5,7 @@ pub mod d4;
|
||||
pub mod d5;
|
||||
pub mod d6;
|
||||
pub mod d7;
|
||||
// pub mod d8;
|
||||
pub mod d8;
|
||||
// pub mod d9;
|
||||
// pub mod d10;
|
||||
// pub mod d11;
|
||||
|
||||
94
src/days/d8.rs
Normal file
94
src/days/d8.rs
Normal file
@ -0,0 +1,94 @@
|
||||
use crate::prelude::*;
|
||||
|
||||
pub type I = Vec<Inst>;
|
||||
pub type O = usize;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum Inst {
|
||||
Rect(usize, usize),
|
||||
RotateY(usize, usize),
|
||||
RotateX(usize, usize),
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
impl Inst {
|
||||
fn from_line (data: &str) -> Self {
|
||||
if let Some(suffix) = data.strip_prefix("rotate row y=") && let Some((y, rotation)) = suffix.split_once(" by ") {
|
||||
Self::RotateY(y.parse().unwrap(), rotation.parse().unwrap())
|
||||
} else if let Some(suffix) = data.strip_prefix("rotate column x=") && let Some((x, rotation)) = suffix.split_once(" by ") {
|
||||
Self::RotateX(x.parse().unwrap(), rotation.parse().unwrap())
|
||||
} else {
|
||||
let (a, b) = data.strip_prefix("rect ").unwrap().split_once('x').unwrap();
|
||||
Self::Rect(a.parse().unwrap(), b.parse().unwrap())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn _parse (data: &str) -> I {
|
||||
data.lines().map(Inst::from_line).collect()
|
||||
}
|
||||
|
||||
fn _solve (data: &I) -> Vec<Vec<bool>> {
|
||||
let mut state = vec![vec![false; 50]; 6];
|
||||
|
||||
for i in data {
|
||||
match i {
|
||||
Inst::Rect(a, b) => {
|
||||
for y in 0..*b {
|
||||
for x in 0..*a {
|
||||
state[y][x] = true;
|
||||
}
|
||||
}
|
||||
},
|
||||
Inst::RotateY(y, r) => {
|
||||
state[*y].rotate_right(*r);
|
||||
},
|
||||
Inst::RotateX(x, r) => {
|
||||
let mut view = state.iter().map(|e| e[*x]).collect_vec();
|
||||
view.rotate_right(*r);
|
||||
|
||||
for (y, n) in view.into_iter().enumerate() {
|
||||
state[y][*x] = n;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
state
|
||||
}
|
||||
|
||||
fn _silver (data: &I) -> O {
|
||||
_solve(data).into_iter().flatten().filter(|e| *e).count()
|
||||
}
|
||||
|
||||
fn _gold (data: &I) -> () {
|
||||
let data = _solve(data).into_iter().map(|line| line.into_iter().map(|f| if f { 'X' } else { '.' }).collect::<String>()).collect_vec();
|
||||
println!("{:#?}", data);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
||||
fn read () -> I {
|
||||
let data = inc!(8);
|
||||
_parse(data)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn silver () {
|
||||
let data = read();
|
||||
let ans = _silver(&data);
|
||||
|
||||
assert_eq!(ans, 115)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gold () {
|
||||
let data = read();
|
||||
let ans = _gold(&data);
|
||||
// EFEYKFRFIJ
|
||||
assert_eq!(ans, ())
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user