From ee6e752de4be6bbe1e1fc4713504be068373a140 Mon Sep 17 00:00:00 2001 From: Irene Knapp Date: Thu, 10 Dec 2020 23:23:14 -0800 Subject: 11 --- 11/Cargo.toml | 11 +++ 11/input | 98 ++++++++++++++++++++ 11/input.small | 10 ++ 11/src/main.rs | 275 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 11/tests/main.rs | 14 +++ Cargo.lock | 8 ++ Cargo.nix | 35 +++++++ Cargo.toml | 1 + 8 files changed, 452 insertions(+) create mode 100644 11/Cargo.toml create mode 100644 11/input create mode 100644 11/input.small create mode 100644 11/src/main.rs create mode 100644 11/tests/main.rs diff --git a/11/Cargo.toml b/11/Cargo.toml new file mode 100644 index 0000000..c35a8db --- /dev/null +++ b/11/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "advent_11" +version = "0.1.0" +authors = ["Irene Knapp "] +edition = "2018" + +[dependencies] +advent_lib = { path = "../lib" } + +[dev-dependencies] +assert_cmd = "0.10" diff --git a/11/input b/11/input new file mode 100644 index 0000000..6a466bd --- /dev/null +++ b/11/input @@ -0,0 +1,98 @@ +LLLLL.LLLLLLLLLLLLLLLLLLLLLLLLLL.LLLLLLL.LLLLLLLLLL.LLLLLLLLLLL.LLLLLLLLLLLLLL.LLLL.LLLL.LLLLLL +LLLLL.LLLLLLLLLLLLLLLL.LLLLL.LLL.LLLLLLL.LLLLL.LLLL.LLLL.LLLLLLLLLLLLLLLL.LLLL.LLLL.LLLL.LLLLLL +LLLLL.LLLL.LLLL.LLLLLL.LLLLLLLLL.LLLLLLL.LLLLLLLLLLLLLLLLLLLLLL.LLLLLLLLL.LLLLLLLLL.LLLLLLLLLLL +LLLLL.LLLL.LLLL.LLLLLLLLLLLLLLLL.LLLLLLL.LLLLL.LLLLLLLLL.LLLLLLLLLLLLLLLL.LLLLLLLLL.LLLL.LLLLLL +LLLLL.LLLL.LLLL.LLLLLL.LLLLLLLLL.LLLLLLL.LLLLL.LLLLLLLLLLLLLLLL.LLLLLLLLL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L.LLLL.LLLLLL +LLLLL.LLLL.LLLL.LLLLLL.LLLLLLLLL.LLLLLLLLLLLLLLLLLL.LL.L.LLLLLLLLLLLLLLLL.LLLL.LLLLLLLLL.LLLLLL +.L..L...L........L.......L.L.LL.LLLL...LLL.L.L..L.L....L.................L.L.L.L....L...L...L.. +LLLLL.LLLLLLLLLLLLLLLL.LLLLLLLLL.LLLLLLLLLLLLLLLLLL.LLLL.LLLLLL.LLLLLLLLLLLLLL.LLLL.LLLL.LLLLLL +LLLLLLLLLL.LLLL.LLLLLL.LLLLLLLLLLLLLLLLLLLLLLL.LLLL.LLLL.LLLLLL.LLLLLLLLLLLLLL.LLLL.LLLL.LLLLLL +LLLLL.LLLL.LLLL.LLLLLL.LLLLLLLLL.LLLLLLL.LLLLL.LLLL.LLLL.LLLLLLLLLLLLLLLLLLLLL.LLLL.LLLL.LLLLLL +LLLLL.LLLL.LLLL.LLLLLL.LLLLLLLLL.LLLLLLL.LLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLL.LLLL.LLLL.LLLLLL.LLLL +LLLLL.LL.LLLLLLL.LLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLL.LLLL.LLLLLLLLLLLLLLLL.LLLL.LLLLLLLLL.LLLLLL +LLLLL.LLLLLLLLL.LLLLLL.LLLLLLLLL.LLLLLLLLLLLLL.LLLL.LLLLLLLLLLL.LLLLLLLLLLLLLL.LLLL.LLLL.LLLLLL +LLLL...LLL.......LL..L.L.L.L...L........LL..............L.L......L.......L..LL....L....LL...LL. +LLLLL.LLLL.LLLL.LLLLLL.LLLLLLLLL.LLLLLLLLLLLLLLLLLL.LLLL.LLLLLL.L.LLLLLLL.LLLLLLLLL.LLLLLLLLLLL +LLLLL.LLLL.LLLL.LLLLLL..LLLLLLLL.LLLLLLLLLLLLLLLLLL.LLLL.LLLLLL.LLLLLLLLL.LLLLLLL.L.LLLL.LLLLLL +LLLLL.LLLL.LLLLLLLLLLL.LLLLLLLLL.LLLLLLL.LLLLL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LLL.LLLLLLLLL.LLLLLL.LLLLLLLLLLLLLLLLL.LLLL..LLLL.LLLL.LLLLLL.LLLLLLLLL.LLLL.LLLLLLLLL.LLLLLL +LLLLL.LLLL.LLLL.LLLLLL.LLLLLLLLL.LLLLLLL.LLLLL.LLLL.LLLLLLLLLLL.LLLLLLLLLLLLLL.LLLLLLLLL.LLLLLL +LLLLL.LLLL.LLLL.LLLLLLLLLLLLLLLL.LLLLLLLLLLLLL.LLLL.LLLL.LLLLLLLLLLLLLLLLLLLLLLLLLL.LLLLLLLLLLL +LLLLL.LLL..LLLL.LLLLLL.LLLLLLLLL.LLLLLLL.LLLLL.LLLL.LLLLLLLLLLL.LLLLLLLLL..LLL.LLLLLLLLLLLLLLLL +.LLL......L.L.L......L.....LL......L.LLL.LLL..LL...L.L.......L..L.......L....L.....L.......LL.. +LLLLL.LLLL.LLLL.LLLLLL.LLLLLLLLL.LLLLLLL.LLLLL.LLLL.LLLL.LLLLLLLLLLLLLLLL.LLLL.LLLLLLLLL.LLLLLL +LLLLL.LLLL.LLLLLLLLLLLLLLLLLLLLL.LLLLLLL.LLLLL.LLLL.LLLL.LLLLLL.LLLLLLLLL.LLLL.LLLL.LLLL.LLLLLL +LLLLLLLLLL.LLLL.LLLLLL.LLLLLLLLL.LLLLLLL.LLLLLLLLLLLLLLL.LLLLLL.LLLLLLLLL.LLLL.LLLL.LLLL.LLLLLL +LLLLL.LLLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLLLLLLL.LLLLLLLLL.LLLLLL.LLLLLLLLL.LLLLLLLLLLLLLL.LLLLLL +LLLLL.LLLLLLLLL.LLLLLL.LLLLLLLLLLLLLLLLLLLLLLL.LLLL.LLLL.LLLLLLLLLLLLLLLL.LLLL.LLLL.LLLL.LLLLLL +LL...L...........L...L..L......LL...........L...L.LL..LL....L....LLL.LLLL....LLL...LL..L..L...L +LLLLLLLLLLLLLLL.LLLLLL.LLLLLLLLL.LLLLLLLLLLLLL.LLLL.LLLL.LLLLL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LLL.LLLL.LLLLLL +LLLLL.LLLL.LLLL.LLLLLL.LLLLLLLLL.LLLLLLL.LLLLL.LLLL.LLLL.LLLLLLLLLLLLLLLL.LLLL.LLLL.LLLL.LLLLLL +LLLLL.LLLLLLLLLLLLLLLL.LLLLLLLLL.LLLLLLL.LLLLL.LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL.LLLLLLLLL.LLLLLL diff --git a/11/input.small b/11/input.small new file mode 100644 index 0000000..1beaede --- /dev/null +++ b/11/input.small @@ -0,0 +1,10 @@ +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 diff --git a/11/src/main.rs b/11/src/main.rs new file mode 100644 index 0000000..1ea6365 --- /dev/null +++ b/11/src/main.rs @@ -0,0 +1,275 @@ +use advent_lib::prelude::*; + +use std::convert::TryFrom; + + +#[derive(Clone,Debug,PartialEq)] +pub enum CellState { + Floor, + EmptySeat, + FullSeat, +} + + +pub fn debug_grid(grid: &Vec>) { + let mut output = String::new(); + + for row in grid { + for cell in row { + match cell { + CellState::Floor => { + output.push_str("."); + } + CellState::EmptySeat => { + output.push_str("L"); + } + CellState::FullSeat => { + output.push_str("#"); + } + } + } + output.push_str("\n"); + } + output.push_str("\n"); + println!("{}", output); +} + + +fn main() -> Result<()> { + let mut args = std::env::args(); + if args.len() != 2 { + eprintln!("Usage: advent input"); + } + let _ = args.next(); + let filename = args.next().unwrap(); + + let input = advent_lib::read_lines_file(&filename)?; + + let mut original_grid: Vec> = Vec::new(); + + for line in &input { + let mut row: Vec = Vec::new(); + + for c in line.chars() { + if c == 'L' { + row.push(CellState::EmptySeat); + } else { + row.push(CellState::Floor); + } + } + + original_grid.push(row); + } + + let mut grid = original_grid.clone(); + let nearby_seats = compute_nearby_seats(&original_grid); + + loop { + let (any_changed, new_grid) = simulate_one(&grid); + if !any_changed { + break; + } + grid = new_grid; + } + + println!("{}", count_full_seats(&grid)); + + let mut grid = original_grid.clone(); + + let mut n_iterations = 0; + + loop { + let (any_changed, new_grid) = simulate_distancey_one(&grid, &nearby_seats); + if !any_changed { + break; + } + grid = new_grid; + n_iterations += 1; + if n_iterations % 1000 == 0 { + println!("iteration {}", n_iterations); + debug_grid(&grid); + } + } + + println!("{}", count_full_seats(&grid)); + + Ok(()) +} + + +fn simulate_one(input_grid: &Vec>) -> (bool, Vec>) { + let mut output_grid: Vec> = Vec::new(); + let mut any_changed = false; + + for y in 0 .. input_grid.len() { + let mut output_row: Vec = Vec::new(); + + for x in 0 .. input_grid[y].len() { + let mut adjacent_count = 0; + + let mut nearby_x_min = x; + if nearby_x_min > 0 { + nearby_x_min -= 1; + } + + let mut nearby_x_max = x; + if nearby_x_max + 1 < input_grid[y].len() { + nearby_x_max += 1; + } + + let mut nearby_y_min = y; + if nearby_y_min > 0 { + nearby_y_min -= 1; + } + + let mut nearby_y_max = y; + if nearby_y_max + 1 < input_grid.len() { + nearby_y_max += 1; + } + + for nearby_x in nearby_x_min .. nearby_x_max + 1 { + for nearby_y in nearby_y_min .. nearby_y_max + 1 { + if nearby_x == x && nearby_y == y { + continue; + } + + let nearby_cell = &input_grid[nearby_y][nearby_x]; + + if *nearby_cell == CellState::FullSeat { + adjacent_count += 1; + } + } + } + + let input_cell = &input_grid[y][x]; + if *input_cell == CellState::Floor { + output_row.push(CellState::Floor); + } else if *input_cell == CellState::EmptySeat && adjacent_count == 0 { + output_row.push(CellState::FullSeat); + any_changed = true; + } else if *input_cell == CellState::FullSeat && adjacent_count >= 4 { + output_row.push(CellState::EmptySeat); + any_changed = true; + } else { + output_row.push(input_cell.clone()); + } + } + + output_grid.push(output_row); + } + + (any_changed, output_grid) +} + + +fn simulate_distancey_one( + input_grid: &Vec>, + nearby_seats: &Vec>>) + -> (bool, Vec>) +{ + let mut output_grid: Vec> = Vec::new(); + let mut any_changed = false; + + for y in 0 .. input_grid.len() { + let mut output_row: Vec = Vec::new(); + + for x in 0 .. input_grid[y].len() { + let mut adjacent_count = 0; + + for (nearby_x, nearby_y) in &nearby_seats[y][x] { + let nearby_cell = &input_grid[*nearby_y][*nearby_x]; + + if *nearby_cell == CellState::FullSeat { + adjacent_count += 1; + } + } + + let input_cell = &input_grid[y][x]; + if *input_cell == CellState::Floor { + output_row.push(CellState::Floor); + } else if *input_cell == CellState::EmptySeat && adjacent_count == 0 { + output_row.push(CellState::FullSeat); + any_changed = true; + } else if *input_cell == CellState::FullSeat && adjacent_count >= 5 { + output_row.push(CellState::EmptySeat); + any_changed = true; + } else { + output_row.push(input_cell.clone()); + } + } + + output_grid.push(output_row); + } + + (any_changed, output_grid) +} + + +fn compute_nearby_seats(input_grid: &Vec>) + -> Vec>> +{ + let mut nearby_seats: Vec>> = Vec::new(); + + let mut max_dimension: isize = isize::try_from(input_grid.len()).unwrap(); + if isize::try_from(input_grid[0].len()).unwrap() > max_dimension { + max_dimension = isize::try_from(input_grid[0].len()).unwrap(); + } + + for y in 0 .. input_grid.len() { + let mut nearby_seats_row: Vec> = Vec::new(); + + for x in 0 .. input_grid[y].len() { + let mut nearby_seats_one: Vec<(usize, usize)> = Vec::new(); + + for (dx, dy) in [ + (-1, -1), (0, -1), (1, -1), (-1, 0), (1, 0), (-1, 1), (0, 1), (1, 1) ].iter() + { + for distance in 1 .. max_dimension { + let nearby_y = isize::try_from(y).unwrap() + dy * distance; + let nearby_x = isize::try_from(x).unwrap() + dx * distance; + + if nearby_y < 0 || usize::try_from(nearby_y).unwrap() >= input_grid.len() { + break; + } + + if nearby_x < 0 || usize::try_from(nearby_x).unwrap() + >= input_grid[usize::try_from(nearby_y).unwrap()].len() + { + break; + } + + let nearby_cell = &input_grid[usize::try_from(nearby_y).unwrap()] + [usize::try_from(nearby_x).unwrap()]; + + if *nearby_cell == CellState::FullSeat || *nearby_cell == CellState::EmptySeat { + nearby_seats_one.push((usize::try_from(nearby_x).unwrap(), + usize::try_from(nearby_y).unwrap())); + break; + } + } + } + + nearby_seats_row.push(nearby_seats_one); + } + + nearby_seats.push(nearby_seats_row); + } + + nearby_seats +} + + +fn count_full_seats(grid: &Vec>) -> i64 { + let mut count = 0; + + for row in grid { + for cell in row { + if *cell == CellState::FullSeat { + count += 1; + } + } + } + + count +} + diff --git a/11/tests/main.rs b/11/tests/main.rs new file mode 100644 index 0000000..c09af93 --- /dev/null +++ b/11/tests/main.rs @@ -0,0 +1,14 @@ +use assert_cmd::prelude::*; +use std::process::Command; + + +#[test] +fn personal_input() -> Result<(), Box> { + let mut command = Command::cargo_bin("advent_11")?; + + command.arg("input"); + command.assert().success().stdout("2406\n2149\n"); + + Ok(()) +} + diff --git a/Cargo.lock b/Cargo.lock index 272770c..1c31fa8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -82,6 +82,14 @@ dependencies = [ "assert_cmd", ] +[[package]] +name = "advent_11" +version = "0.1.0" +dependencies = [ + "advent_lib", + "assert_cmd", +] + [[package]] name = "advent_lib" version = "0.1.0" diff --git a/Cargo.nix b/Cargo.nix index f5b6f81..785f38c 100644 --- a/Cargo.nix +++ b/Cargo.nix @@ -128,6 +128,16 @@ rec { # File a bug if you depend on any for non-debug work! debug = internal.debugCrate { inherit packageId; }; }; + "advent_11" = rec { + packageId = "advent_11"; + build = internal.buildRustCrateWithFeatures { + packageId = "advent_11"; + }; + + # Debug support which might change between releases. + # File a bug if you depend on any for non-debug work! + debug = internal.debugCrate { inherit packageId; }; + }; "advent_lib" = rec { packageId = "advent_lib"; build = internal.buildRustCrateWithFeatures { @@ -417,6 +427,31 @@ rec { } ]; + }; + "advent_11" = rec { + crateName = "advent_11"; + version = "0.1.0"; + edition = "2018"; + crateBin = [ + { name = "advent_11"; path = "src/main.rs"; } + ]; + src = (builtins.filterSource sourceFilter ./11); + authors = [ + "Irene Knapp " + ]; + dependencies = [ + { + name = "advent_lib"; + packageId = "advent_lib"; + } + ]; + devDependencies = [ + { + name = "assert_cmd"; + packageId = "assert_cmd"; + } + ]; + }; "advent_lib" = rec { crateName = "advent_lib"; diff --git a/Cargo.toml b/Cargo.toml index 35009be..abc26f4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,4 +11,5 @@ members = [ "08", "09", "10", + "11", ] -- cgit 1.4.1