diff options
author | Justin Wernick <justin@worthe-it.co.za> | 2022-04-19 20:33:35 +0200 |
---|---|---|
committer | Justin Wernick <justin@worthe-it.co.za> | 2022-04-19 20:33:35 +0200 |
commit | 80b340f2ff9a4639d67b5a3e1a43e5b1b292c1ca (patch) | |
tree | 12ce533aa04561b317b8cc3779609b29dc31482a /2021/src/bin/day_11.rs | |
parent | 2a939b5e97604d3129b888f15c9876ffd3a7fe5a (diff) | |
parent | 6d72191e2ce5d423ca03c894d2dad1d3061bd4f3 (diff) |
Merge branch '2021-main'
Diffstat (limited to '2021/src/bin/day_11.rs')
-rw-r--r-- | 2021/src/bin/day_11.rs | 130 |
1 files changed, 130 insertions, 0 deletions
diff --git a/2021/src/bin/day_11.rs b/2021/src/bin/day_11.rs new file mode 100644 index 0000000..2a4cd89 --- /dev/null +++ b/2021/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) +} |