adventofcode-2020/day-11/src/main.rs

122 lines
2.8 KiB
Rust

use std::fs;
use std::iter;
use ndarray::Array2;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let input = fs::read_to_string("input")?;
let layout = parse_layout(&input)?;
// Part 1
let result = count_with_neighboring_seats(&layout);
println!("{}", result);
// Part 2
let result = count_with_directional_seats(&layout);
println!("{}", result);
Ok(())
}
type Layout = Array2<Seating>;
#[derive(Copy, Clone, Debug, PartialEq)]
enum Seating {
Occupied,
Empty,
Floor,
}
impl Seating {
fn new(symbol: char) -> Result<Self, String> {
match symbol {
'#' => Ok(Seating::Occupied),
'L' => Ok(Seating::Empty),
'.' => Ok(Seating::Floor),
rubbish => Err(format!("Found some rubbish on the floor, area must be clean to conserve human behavior: {}", rubbish))
}
}
}
fn parse_layout(input: &str) -> Result<Layout, String> {
let (i, j) = (
input.lines().count(),
input.lines().next().ok_or("Waiting room too small")?.len(),
);
// Padding the input data so that we can later comfortably produce
// a convolution
let grid = input
.lines()
.flat_map(|line| {
iter::once(Ok(Seating::Floor))
.chain(line.chars().map(Seating::new))
.chain(iter::once(Ok(Seating::Floor)))
})
.collect::<Result<Vec<Seating>, String>>()?;
Array2::from_shape_vec((i, j), grid)
.map_err(|_| "Non-square waiting rooms don't exist".to_string())
}
fn count_with_neighboring_seats(layout: &Layout) -> usize {
unimplemented!()
}
fn count_with_directional_seats(layout: &Layout) -> usize {
unimplemented!()
}
#[cfg(test)]
mod tests {
use super::*;
use indoc::indoc;
#[test]
fn test_simple() -> Result<(), Box<dyn std::error::Error>> {
let input = indoc!(
"L.LL.LL.LL
LLLLLLL.LL
L.L.L..L..
LLLL.LL.LL
L.LL.LL.LL
L.LLLLL.LL
..L.L.....
LLLLLLLLLL
L.LLLLLL.L
L.LLLLL.LL
"
);
let layout = parse_layout(&input)?;
let result = count_with_neighboring_seats(&layout);
assert_eq!(result, 37);
Ok(())
}
#[test]
fn test_simple2() -> Result<(), Box<dyn std::error::Error>> {
let input = indoc!(
"L.LL.LL.LL
LLLLLLL.LL
L.L.L..L..
LLLL.LL.LL
L.LL.LL.LL
L.LLLLL.LL
..L.L.....
LLLLLLLLLL
L.LLLLLL.L
L.LLLLL.LL
"
);
let layout = parse_layout(&input)?;
let result = count_with_directional_seats(&layout);
assert_eq!(result, 37);
Ok(())
}
}