commit 7dda43d51a149382991f9c053a1963fab85b21c3
parent ec29c015d2e9d27129490ecb55969acb371943f7
Author: Louis Burda <quent.burda@gmail.com>
Date: Tue, 14 Dec 2021 01:07:40 +0100
Add day 11 solution
Diffstat:
6 files changed, 502 insertions(+), 0 deletions(-)
diff --git a/src/11/Cargo.toml b/src/11/Cargo.toml
@@ -0,0 +1,7 @@
+[package]
+name = "aoc"
+version = "0.1.0"
+edition = "2021"
+
+[dependencies]
+
diff --git a/src/11/input b/src/11/input
@@ -0,0 +1,10 @@
+6227618536
+2368158384
+5385414113
+4556757523
+6746486724
+4881323884
+4648263744
+4871332872
+4724128228
+4316512167
diff --git a/src/11/input.test b/src/11/input.test
@@ -0,0 +1,10 @@
+5483143223
+2745854711
+5264556173
+6141336146
+6357385478
+4167524645
+2176841721
+6882881134
+4846848554
+5283751526
diff --git a/src/11/part1 b/src/11/part1
@@ -0,0 +1,320 @@
+--- Day 11: Dumbo Octopus ---
+
+You enter a large cavern full of rare bioluminescent dumbo octopuses! They seem to not like the
+Christmas lights on your submarine, so you turn them off for now.
+
+There are 100 octopuses arranged neatly in a 10 by 10 grid. Each octopus slowly gains
+[1m[37menergy[0m over time and [1m[37mflashes[0m brightly for a moment when its energy is full. Although your lights are
+off, maybe you could navigate through the cave without disturbing the octopuses if you could predict
+when the flashes of light will happen.
+
+Each octopus has an [1m[37menergy level[0m - your submarine can remotely measure the energy level of each
+octopus (your puzzle input). For example:
+
+5483143223
+2745854711
+5264556173
+6141336146
+6357385478
+4167524645
+2176841721
+6882881134
+4846848554
+5283751526
+
+The energy level of each octopus is a value between 0 and 9. Here, the top-left octopus has an
+energy level of 5, the bottom-right one has an energy level of 6, and so on.
+
+You can model the energy levels and flashes of light in [1m[37msteps[0m. During a single step, the following
+occurs:
+
+
+ - First, the energy level of each octopus increases by 1.
+
+ - Then, any octopus with an energy level greater than 9 [1m[37mflashes[0m. This increases the energy level of
+all adjacent octopuses by 1, including octopuses that are diagonally adjacent. If this causes an
+octopus to have an energy level greater than 9, it [1m[37malso flashes[0m. This process continues as long as
+new octopuses keep having their energy level increased beyond 9. (An octopus can only flash
+[1m[37mat most once per step[0m.)
+
+ - Finally, any octopus that flashed during this step has its energy level set to 0, as it used all
+of its energy to flash.
+
+
+Adjacent flashes can cause an octopus to flash on a step even if it begins that step with very
+little energy. Consider the middle octopus with 1 energy in this situation:
+
+Before any steps:
+11111
+19991
+19191
+19991
+11111
+
+After step 1:
+34543
+4[1m[37m000[0m4
+5[1m[37m000[0m5
+4[1m[37m000[0m4
+34543
+
+After step 2:
+45654
+51115
+61116
+51115
+45654
+
+An octopus is [1m[37mhighlighted[0m when it flashed during the given step.
+
+Here is how the larger example above progresses:
+
+Before any steps:
+5483143223
+2745854711
+5264556173
+6141336146
+6357385478
+4167524645
+2176841721
+6882881134
+4846848554
+5283751526
+
+After step 1:
+6594254334
+3856965822
+6375667284
+7252447257
+7468496589
+5278635756
+3287952832
+7993992245
+5957959665
+6394862637
+
+After step 2:
+88[1m[37m0[0m7476555
+5[1m[37m0[0m89[1m[37m0[0m87[1m[37m0[0m54
+85978896[1m[37m0[0m8
+84857696[1m[37m00[0m
+87[1m[37m00[0m9[1m[37m0[0m88[1m[37m00[0m
+66[1m[37m000[0m88989
+68[1m[37m0000[0m5943
+[1m[37m000000[0m7456
+9[1m[37m000000[0m876
+87[1m[37m0000[0m6848
+
+After step 3:
+[1m[37m00[0m5[1m[37m0[0m9[1m[37m00[0m866
+85[1m[37m00[0m8[1m[37m00[0m575
+99[1m[37m000000[0m39
+97[1m[37m000000[0m41
+9935[1m[37m0[0m8[1m[37m00[0m63
+77123[1m[37m00000[0m
+791125[1m[37m000[0m9
+221113[1m[37m0000[0m
+[1m[37m0[0m421125[1m[37m000[0m
+[1m[37m00[0m21119[1m[37m000[0m
+
+After step 4:
+2263[1m[37m0[0m31977
+[1m[37m0[0m923[1m[37m0[0m31697
+[1m[37m00[0m3222115[1m[37m0[0m
+[1m[37m00[0m41111163
+[1m[37m00[0m76191174
+[1m[37m00[0m53411122
+[1m[37m00[0m4236112[1m[37m0[0m
+5532241122
+1532247211
+113223[1m[37m0[0m211
+
+After step 5:
+4484144[1m[37m000[0m
+2[1m[37m0[0m44144[1m[37m000[0m
+2253333493
+1152333274
+11873[1m[37m0[0m3285
+1164633233
+1153472231
+6643352233
+2643358322
+2243341322
+
+After step 6:
+5595255111
+3155255222
+33644446[1m[37m0[0m5
+2263444496
+2298414396
+2275744344
+2264583342
+7754463344
+3754469433
+3354452433
+
+After step 7:
+67[1m[37m0[0m7366222
+4377366333
+4475555827
+34966557[1m[37m0[0m9
+35[1m[37m00[0m6256[1m[37m0[0m9
+35[1m[37m0[0m9955566
+3486694453
+8865585555
+486558[1m[37m0[0m644
+4465574644
+
+After step 8:
+7818477333
+5488477444
+5697666949
+46[1m[37m0[0m876683[1m[37m0[0m
+473494673[1m[37m0[0m
+474[1m[37m00[0m97688
+69[1m[37m0000[0m7564
+[1m[37m000000[0m9666
+8[1m[37m00000[0m4755
+68[1m[37m0000[0m7755
+
+After step 9:
+9[1m[37m0[0m6[1m[37m0000[0m644
+78[1m[37m00000[0m976
+69[1m[37m000000[0m8[1m[37m0[0m
+584[1m[37m00000[0m82
+5858[1m[37m0000[0m93
+69624[1m[37m00000[0m
+8[1m[37m0[0m2125[1m[37m000[0m9
+222113[1m[37m000[0m9
+9111128[1m[37m0[0m97
+7911119976
+
+After step 10:
+[1m[37m0[0m481112976
+[1m[37m00[0m31112[1m[37m00[0m9
+[1m[37m00[0m411125[1m[37m0[0m4
+[1m[37m00[0m811114[1m[37m0[0m6
+[1m[37m00[0m991113[1m[37m0[0m6
+[1m[37m00[0m93511233
+[1m[37m0[0m44236113[1m[37m0[0m
+553225235[1m[37m0[0m
+[1m[37m0[0m53225[1m[37m0[0m6[1m[37m00[0m
+[1m[37m00[0m3224[1m[37m0000[0m
+
+After step 10, there have been a total of 204 flashes. Fast forwarding, here is the same
+configuration every 10 steps:
+
+After step 20:
+3936556452
+56865568[1m[37m0[0m6
+449655569[1m[37m0[0m
+444865558[1m[37m0[0m
+445686557[1m[37m0[0m
+568[1m[37m00[0m86577
+7[1m[37m00000[0m9896
+[1m[37m0000000[0m344
+6[1m[37m000000[0m364
+46[1m[37m0000[0m9543
+
+After step 30:
+[1m[37m0[0m643334118
+4253334611
+3374333458
+2225333337
+2229333338
+2276733333
+2754574565
+5544458511
+9444447111
+7944446119
+
+After step 40:
+6211111981
+[1m[37m0[0m421111119
+[1m[37m00[0m42111115
+[1m[37m000[0m3111115
+[1m[37m000[0m3111116
+[1m[37m00[0m65611111
+[1m[37m0[0m532351111
+3322234597
+2222222976
+2222222762
+
+After step 50:
+9655556447
+48655568[1m[37m0[0m5
+448655569[1m[37m0[0m
+445865558[1m[37m0[0m
+457486557[1m[37m0[0m
+57[1m[37m000[0m86566
+6[1m[37m00000[0m9887
+8[1m[37m000000[0m533
+68[1m[37m00000[0m633
+568[1m[37m0000[0m538
+
+After step 60:
+25333342[1m[37m00[0m
+274333464[1m[37m0[0m
+2264333458
+2225333337
+2225333338
+2287833333
+3854573455
+1854458611
+1175447111
+1115446111
+
+After step 70:
+8211111164
+[1m[37m0[0m421111166
+[1m[37m00[0m42111114
+[1m[37m000[0m4211115
+[1m[37m0000[0m211116
+[1m[37m00[0m65611111
+[1m[37m0[0m532351111
+7322235117
+5722223475
+4572222754
+
+After step 80:
+1755555697
+59655556[1m[37m0[0m9
+448655568[1m[37m0[0m
+445865558[1m[37m0[0m
+457[1m[37m0[0m86557[1m[37m0[0m
+57[1m[37m000[0m86566
+7[1m[37m00000[0m8666
+[1m[37m0000000[0m99[1m[37m0[0m
+[1m[37m0000000[0m8[1m[37m00[0m
+[1m[37m0000000000[0m
+
+After step 90:
+7433333522
+2643333522
+2264333458
+2226433337
+2222433338
+2287833333
+2854573333
+4854458333
+3387779333
+3333333333
+
+After step 100:
+[1m[37m0[0m397666866
+[1m[37m0[0m749766918
+[1m[37m00[0m53976933
+[1m[37m000[0m4297822
+[1m[37m000[0m4229892
+[1m[37m00[0m53222877
+[1m[37m0[0m532222966
+9322228966
+7922286866
+6789998766
+
+After 100 steps, there have been a total of [1m[37m1656[0m flashes.
+
+Given the starting energy levels of the dumbo octopuses in your cavern, simulate 100 steps.
+[1m[37mHow many total flashes are there after 100 steps?[0m
+
+
diff --git a/src/11/part2 b/src/11/part2
@@ -0,0 +1,47 @@
+--- Part Two ---
+
+It seems like the individual flashes aren't bright enough to navigate. However, you might have a
+better option: the flashes seem to be [1m[37msynchronizing[0m!
+
+In the example above, the first time all octopuses flash simultaneously is step [1m[37m195[0m:
+
+After step 193:
+5877777777
+8877777777
+7777777777
+7777777777
+7777777777
+7777777777
+7777777777
+7777777777
+7777777777
+7777777777
+
+After step 194:
+6988888888
+9988888888
+8888888888
+8888888888
+8888888888
+8888888888
+8888888888
+8888888888
+8888888888
+8888888888
+
+After step 195:
+[1m[37m0000000000[0m
+[1m[37m0000000000[0m
+[1m[37m0000000000[0m
+[1m[37m0000000000[0m
+[1m[37m0000000000[0m
+[1m[37m0000000000[0m
+[1m[37m0000000000[0m
+[1m[37m0000000000[0m
+[1m[37m0000000000[0m
+[1m[37m0000000000[0m
+
+If you can calculate the exact moments when the octopuses will all flash simultaneously, you should
+be able to navigate through the cavern. [1m[37mWhat is the first step during which all octopuses flash?[0m
+
+
diff --git a/src/11/src/main.rs b/src/11/src/main.rs
@@ -0,0 +1,108 @@
+use std::fs;
+use std::env;
+
+#[derive(PartialEq,Eq)]
+struct Pos {
+ x: i32,
+ y: i32
+}
+
+const SURROUNDING: [Pos; 8] = [
+ Pos { x: -1, y: -1 },
+ Pos { x: -1, y: 0 },
+ Pos { x: -1, y: 1 },
+ Pos { x: 0, y: -1 },
+ Pos { x: 0, y: 1 },
+ Pos { x: 1, y: -1 },
+ Pos { x: 1, y: 0 },
+ Pos { x: 1, y: 1 }
+];
+
+fn main() {
+ let args: Vec<String> = env::args().collect();
+ if args[1] == "1" {
+ part1();
+ } else if args[1] == "2" {
+ part2();
+ } else {
+ eprintln!("No such part\n");
+ }
+}
+
+fn print_octos(octos: &Vec<Vec<u32>>) {
+ for row in octos {
+ for octo in row {
+ print!("{}", octo);
+ }
+ println!();
+ }
+ println!();
+}
+
+fn inc_check(flashed: &mut Vec<Pos>, octos: &mut Vec<Vec<u32>>, x: usize, y: usize) {
+ let pos = Pos { x: x as i32, y: y as i32 };
+ if flashed.contains(&pos) { return; }
+
+ octos[y][x] += 1;
+ if octos[y][x] < 10 { return; }
+
+ flashed.push(Pos { x: x as i32, y: y as i32 });
+ octos[y][x] -= 10;
+
+ assert!(octos.len() > 0);
+ let height = octos.len();
+ let width = octos[0].len();
+ for d in SURROUNDING {
+ let nx = ((x as i32) + d.x) as usize;
+ let ny = ((y as i32) + d.y) as usize;
+ if (0..width).contains(&nx) && (0..height).contains(&ny) {
+ inc_check(flashed, octos, nx, ny);
+ }
+ }
+}
+
+fn sim_step(octos: &mut Vec<Vec<u32>>) -> usize {
+ let mut flashed: Vec<Pos> = Vec::new();
+
+ assert!(octos.len() > 0);
+ let height = octos.len();
+ let width = octos[0].len();
+ for y in 0..height {
+ for x in 0..width {
+ inc_check(&mut flashed, octos, x, y);
+ }
+ }
+
+ return flashed.len();
+}
+
+fn part1() {
+ let content = fs::read_to_string("input").expect("no file");
+ let mut octos: Vec<Vec<u32>> = content.lines()
+ .map(|l| l.chars().map(|c| c.to_digit(10).unwrap()).collect()).collect();
+
+ let mut flashes: usize = 0;
+ for _i in 0..100 {
+ flashes += sim_step(&mut octos);
+ }
+
+ println!("{}", flashes);
+}
+
+fn part2() {
+ let content = fs::read_to_string("input").expect("no file");
+ let mut octos: Vec<Vec<u32>> = content.lines()
+ .map(|l| l.chars().map(|c| c.to_digit(10).unwrap()).collect()).collect();
+
+ let height = octos.len();
+ let width = octos[0].len();
+
+ let mut step = 0;
+ let mut last = 0;
+ while last != width * height {
+ last = sim_step(&mut octos);
+ step += 1;
+ }
+
+ println!("{}", step);
+}