summary refs log tree commit diff
diff options
context:
space:
mode:
authorIrene Knapp <ireneista@gmail.com>2021-12-10 21:26:33 -0800
committerIrene Knapp <ireneista@gmail.com>2021-12-10 21:26:33 -0800
commite07d2a9191fe5204a3cff7e9a62ba1c7c2d3a618 (patch)
treee7028724fe33763ea6f5f0bb899d48fbc472d240
parent964c72cc4fe03c06bd067fc7605cca96f8b94943 (diff)
11
-rw-r--r--11/Cargo.toml11
-rw-r--r--11/input10
-rw-r--r--11/src/main.rs116
-rw-r--r--11/tests/main.rs17
-rw-r--r--Cargo.lock8
-rw-r--r--Cargo.toml1
6 files changed, 163 insertions, 0 deletions
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 <ireneista@gmail.com>"]
+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<Vec<i64>>) {
+  for row in grid {
+    for cell in row {
+      print!(" {}", cell)
+    }
+    println!("");
+  }
+  println!("");
+}
+
+
+
+fn iterate(grid: &mut Vec<Vec<i64>>) -> 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<dyn std::error::Error>> {
+  let mut command = Command::cargo_bin("advent_11")?;
+
+  command.arg("input");
+  command.assert().success().stdout(
+      "1620\n\
+      371\n");
+
+  Ok(())
+}
+
diff --git a/Cargo.lock b/Cargo.lock
index a8c5722..27fd18a 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -81,6 +81,14 @@ dependencies = [
 ]
 
 [[package]]
+name = "advent_11"
+version = "0.1.0"
+dependencies = [
+ "advent_lib",
+ "assert_cmd",
+]
+
+[[package]]
 name = "advent_lib"
 version = "0.1.0"
 dependencies = [
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",
 ]