summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2021-12-11 00:31:12 -0500
committerJesse Luehrs <doy@tozt.net>2021-12-11 00:31:12 -0500
commit38b6e54beadfdf4f831b8c08f3b0b6f64a441038 (patch)
tree43da997eedefb42b04dfb2af2af1082ff96e894c
parent6f8d0013849efba031e77f60d2396cb187c06dbb (diff)
downloadadvent-of-code-38b6e54beadfdf4f831b8c08f3b0b6f64a441038.tar.gz
advent-of-code-38b6e54beadfdf4f831b8c08f3b0b6f64a441038.zip
day 11
-rw-r--r--data/2021/11.txt10
-rw-r--r--src/2021/11/mod.rs117
-rw-r--r--src/2021/mod.rs4
3 files changed, 131 insertions, 0 deletions
diff --git a/data/2021/11.txt b/data/2021/11.txt
new file mode 100644
index 0000000..cfbdc33
--- /dev/null
+++ b/data/2021/11.txt
@@ -0,0 +1,10 @@
+5421451741
+3877321568
+7583273864
+3451717778
+2651615156
+6377167526
+5182852831
+4766856676
+3437187583
+3633371586
diff --git a/src/2021/11/mod.rs b/src/2021/11/mod.rs
new file mode 100644
index 0000000..8b3370a
--- /dev/null
+++ b/src/2021/11/mod.rs
@@ -0,0 +1,117 @@
+fn adjacent(
+ i: usize,
+ j: usize,
+ max_i: usize,
+ max_j: usize,
+) -> Vec<(usize, usize)> {
+ let mut ret = vec![];
+ if i > 0 {
+ if j > 0 {
+ ret.push((i - 1, j - 1));
+ }
+ ret.push((i - 1, j));
+ if j < max_j {
+ ret.push((i - 1, j + 1));
+ }
+ }
+ if j > 0 {
+ ret.push((i, j - 1));
+ }
+ if j < max_j {
+ ret.push((i, j + 1));
+ }
+ if i < max_i {
+ if j > 0 {
+ ret.push((i + 1, j - 1));
+ }
+ ret.push((i + 1, j));
+ if j < max_j {
+ ret.push((i + 1, j + 1));
+ }
+ }
+ ret
+}
+
+fn iterate(map: &mut Vec<Vec<(i64, bool)>>) -> i64 {
+ let mut flashes = 0;
+ for line in map.iter_mut() {
+ for (cell, _) in line.iter_mut() {
+ *cell += 1;
+ }
+ }
+
+ loop {
+ let mut new_flashes = 0;
+ for i in 0..map.len() {
+ for j in 0..map[0].len() {
+ if map[i][j].1 {
+ continue;
+ }
+ if map[i][j].0 > 9 {
+ map[i][j].1 = true;
+ new_flashes += 1;
+ for (i, j) in
+ adjacent(i, j, map.len() - 1, map[0].len() - 1)
+ {
+ map[i][j].0 += 1;
+ }
+ }
+ }
+ }
+ if new_flashes > 0 {
+ flashes += new_flashes;
+ } else {
+ break;
+ }
+ }
+
+ for line in map.iter_mut() {
+ for (cell, flashed) in line.iter_mut() {
+ if *flashed {
+ *cell = 0;
+ *flashed = false;
+ }
+ }
+ }
+ flashes
+}
+
+pub fn part1() -> anyhow::Result<i64> {
+ let mut map = data_lines!()?
+ .collect::<Result<Vec<String>, _>>()?
+ .iter()
+ .map(|line| {
+ line.bytes()
+ .map(|b| ((b - b'0') as i64, false))
+ .collect::<Vec<_>>()
+ })
+ .collect::<Vec<_>>();
+
+ let mut flashes = 0;
+ for _ in 0..100 {
+ flashes += iterate(&mut map);
+ }
+ Ok(flashes)
+}
+
+pub fn part2() -> anyhow::Result<i64> {
+ let mut map = data_lines!()?
+ .collect::<Result<Vec<String>, _>>()?
+ .iter()
+ .map(|line| {
+ line.bytes()
+ .map(|b| ((b - b'0') as i64, false))
+ .collect::<Vec<_>>()
+ })
+ .collect::<Vec<_>>();
+
+ let mut step = 1;
+ loop {
+ let flashes = iterate(&mut map);
+ if flashes == (map.len() * map[0].len()).try_into()? {
+ break;
+ }
+ step += 1;
+ }
+ Ok(step)
+}
diff --git a/src/2021/mod.rs b/src/2021/mod.rs
index 95346e7..ccac2b0 100644
--- a/src/2021/mod.rs
+++ b/src/2021/mod.rs
@@ -18,6 +18,8 @@ mod day8;
mod day9;
#[path = "10/mod.rs"]
mod day10;
+#[path = "11/mod.rs"]
+mod day11;
// NEXT MOD
pub fn run(day: u8, puzzle: u8) -> anyhow::Result<i64> {
@@ -42,6 +44,8 @@ pub fn run(day: u8, puzzle: u8) -> anyhow::Result<i64> {
(9, 2) => day9::part2(),
(10, 1) => day10::part1(),
(10, 2) => day10::part2(),
+ (11, 1) => day11::part1(),
+ (11, 2) => day11::part2(),
// NEXT PART
_ => Err(anyhow::anyhow!("unknown puzzle {}-{}", day, puzzle)),
}