summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJustin Wernick <justin@worthe-it.co.za>2021-12-11 20:36:30 +0200
committerJustin Wernick <justin@worthe-it.co.za>2021-12-11 20:36:30 +0200
commit5ec36437d7d2b3fca13135872d20748331293283 (patch)
tree632fa5fa1063485747484a820556df49452cf94f
parent92bf39eabf48265ea38b13064ea799d41196bda0 (diff)
Day 11
-rw-r--r--inputs/day_11.txt10
-rw-r--r--src/bin/day_11.rs130
2 files changed, 140 insertions, 0 deletions
diff --git a/inputs/day_11.txt b/inputs/day_11.txt
new file mode 100644
index 0000000..674e736
--- /dev/null
+++ b/inputs/day_11.txt
@@ -0,0 +1,10 @@
+8826876714
+3127787238
+8182852861
+4655371483
+3864551365
+1878253581
+8317422437
+1517254266
+2621124761
+3473331514
diff --git a/src/bin/day_11.rs b/src/bin/day_11.rs
new file mode 100644
index 0000000..2a4cd89
--- /dev/null
+++ b/src/bin/day_11.rs
@@ -0,0 +1,130 @@
+use nom::{
+ character::complete::{line_ending, one_of},
+ combinator::map_res,
+ multi::{many1, separated_list1},
+ IResult,
+};
+use std::fs;
+
+fn main() -> Result<(), Box<dyn std::error::Error>> {
+ let input = fs::read_to_string("inputs/day_11.txt")?;
+ let squid_grid = parse_squid_grid(&input).unwrap().1;
+
+ {
+ let mut squid_grid = squid_grid.clone();
+ let mut flashes_on_100 = 0;
+ for _ in 0..100 {
+ flashes_on_100 += squid_grid.flash();
+ }
+ dbg!(flashes_on_100);
+ }
+
+ {
+ let mut squid_grid = squid_grid.clone();
+ let sync_time = std::iter::repeat_with(|| squid_grid.flash())
+ .position(|flashes| flashes == 100)
+ .map(|x| x + 1);
+ dbg!(sync_time);
+ }
+
+ Ok(())
+}
+
+#[derive(Debug, Clone)]
+struct Squid {
+ energy: u8,
+ has_flashed: bool,
+}
+
+impl Squid {
+ fn should_flash(&self) -> bool {
+ self.energy > 9 && !self.has_flashed
+ }
+
+ fn reset(&mut self) {
+ self.energy = 0;
+ self.has_flashed = false;
+ }
+}
+
+#[derive(Debug, Clone)]
+struct SquidGrid {
+ squids: [[Squid; 10]; 10],
+}
+
+impl SquidGrid {
+ fn flash(&mut self) -> usize {
+ for y in 0..10 {
+ for x in 0..10 {
+ self.squids[y][x].energy += 1;
+ }
+ }
+
+ let mut any_new_flashes = true;
+ while any_new_flashes {
+ any_new_flashes = false;
+ for y in 0..10 {
+ for x in 0..10 {
+ if self.squids[y][x].should_flash() {
+ any_new_flashes = true;
+ self.squids[y][x].has_flashed = true;
+ if y > 0 && x > 0 {
+ self.squids[y - 1][x - 1].energy += 1;
+ }
+ if y > 0 {
+ self.squids[y - 1][x].energy += 1;
+ }
+ if y > 0 && x < 9 {
+ self.squids[y - 1][x + 1].energy += 1;
+ }
+ if x > 0 {
+ self.squids[y][x - 1].energy += 1;
+ }
+ if x < 9 {
+ self.squids[y][x + 1].energy += 1;
+ }
+ if y < 9 && x > 0 {
+ self.squids[y + 1][x - 1].energy += 1;
+ }
+ if y < 9 {
+ self.squids[y + 1][x].energy += 1;
+ }
+ if y < 9 && x < 9 {
+ self.squids[y + 1][x + 1].energy += 1;
+ }
+ }
+ }
+ }
+ }
+
+ let mut flashes = 0;
+ for y in 0..10 {
+ for x in 0..10 {
+ if self.squids[y][x].has_flashed {
+ self.squids[y][x].reset();
+ flashes += 1;
+ }
+ }
+ }
+ flashes
+ }
+}
+
+fn parse_squid_grid(input: &str) -> IResult<&str, SquidGrid> {
+ map_res(separated_list1(line_ending, parse_squid_row), |squids| {
+ squids.try_into().map(|squids| SquidGrid { squids })
+ })(input)
+}
+
+fn parse_squid_row(input: &str) -> IResult<&str, [Squid; 10]> {
+ map_res(many1(parse_squid), |squids| squids.try_into())(input)
+}
+
+fn parse_squid(input: &str) -> IResult<&str, Squid> {
+ map_res(one_of("0123456789"), |digit| {
+ digit.to_string().parse().map(|energy| Squid {
+ energy,
+ has_flashed: false,
+ })
+ })(input)
+}