From e07d2a9191fe5204a3cff7e9a62ba1c7c2d3a618 Mon Sep 17 00:00:00 2001 From: Irene Knapp Date: Fri, 10 Dec 2021 21:26:33 -0800 Subject: 11 --- 11/Cargo.toml | 11 ++++++ 11/input | 10 +++++ 11/src/main.rs | 116 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 11/tests/main.rs | 17 ++++++++ 4 files changed, 154 insertions(+) create mode 100644 11/Cargo.toml create mode 100644 11/input create mode 100644 11/src/main.rs create mode 100644 11/tests/main.rs (limited to '11') 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..e1071b8 --- /dev/null +++ b/11/input @@ -0,0 +1,10 @@ +7147713556 +6167733555 +5183482118 +3885424521 +7533644611 +3877764863 +7636874333 +8687188533 +7467115265 +1626573134 diff --git a/11/src/main.rs b/11/src/main.rs new file mode 100644 index 0000000..566176c --- /dev/null +++ b/11/src/main.rs @@ -0,0 +1,116 @@ +use advent_lib::prelude::*; + + +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 grid = Vec::new(); + for line in &input { + let mut row = Vec::new(); + for c in line.chars() { + let value: i64 = c.to_digit(10).unwrap() as i64; + row.push(value); + } + grid.push(row); + } + + let mut n_flashes = 0; + for _ in 0 .. 100 { + n_flashes += iterate(&mut grid); + } + println!("{}", n_flashes); + + for i in 101 .. 1000000 { + if iterate(&mut grid) == 100 { + println!("{}", i); + break; + } + } + + Ok(()) +} + + +#[allow(dead_code)] +fn debug_grid(grid: &Vec>) { + for row in grid { + for cell in row { + print!(" {}", cell) + } + println!(""); + } + println!(""); +} + + + +fn iterate(grid: &mut Vec>) -> i64 { + let height = grid.len(); + let width = grid[0].len(); + + let mut has_flashed = Vec::new(); + for _ in 0 .. height { + let mut has_flashed_row = Vec::new(); + for _ in 0 .. width { + has_flashed_row.push(false); + } + has_flashed.push(has_flashed_row); + } + + for y in 0 .. height { + for x in 0 .. width { + grid[y][x] += 1; + } + } + + let mut n_flashes = 0; + loop { + let mut any_flashes = false; + + for y in 0 .. height { + for x in 0 .. width { + if (grid[y][x] > 9) && !has_flashed[y][x] { + n_flashes += 1; + has_flashed[y][x] = true; + any_flashes = true; + + for (dx, dy) in &[(-1, -1), (0, -1), (1, -1), + (-1, 0), (1, 0), + (-1, 1), (0, 1), (1, 1)] + { + let adjacent_x = x as i64 + dx; + let adjacent_y = y as i64 + dy; + + if (adjacent_x >= 0) && (adjacent_x < width as i64) + && (adjacent_y >= 0) && (adjacent_y < height as i64) + { + grid[adjacent_y as usize][adjacent_x as usize] += 1; + } + } + } + } + } + + if !any_flashes { + break; + } + } + + for y in 0 .. height { + for x in 0 .. width { + if has_flashed[y][x] { + grid[y][x] = 0; + } + } + } + + n_flashes +} + diff --git a/11/tests/main.rs b/11/tests/main.rs new file mode 100644 index 0000000..d0ffe86 --- /dev/null +++ b/11/tests/main.rs @@ -0,0 +1,17 @@ +use assert_cmd::prelude::*; +//use predicates::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( + "1620\n\ + 371\n"); + + Ok(()) +} + -- cgit 1.4.1