From cfa268a709618b1316eafc766b79ef3d7fab0b70 Mon Sep 17 00:00:00 2001 From: BakerNet Date: Wed, 20 Dec 2023 23:15:43 -0800 Subject: [PATCH] Day 21... tapped out on part 2 --- README.md | 2 +- data/examples/21.txt | 11 +++++++ src/bin/21.rs | 73 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 data/examples/21.txt create mode 100644 src/bin/21.rs diff --git a/README.md b/README.md index fd5c316..027df7a 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ Solutions for [Advent of Code](https://adventofcode.com/) in [Rust](https://www. | [Day 18](https://adventofcode.com/2023/day/18) | [code](src/bin/18.rs) | ⭐ | ⭐ | | [Day 19](https://adventofcode.com/2023/day/19) | [code](src/bin/19.rs) | ⭐ | ⭐ | | [Day 20](https://adventofcode.com/2023/day/20) | [code](src/bin/20.rs) | ⭐ | ⭐ | -| [Day 21](https://adventofcode.com/2023/day/21) | [code](src/bin/21.rs) | | | +| [Day 21](https://adventofcode.com/2023/day/21) | [code](src/bin/21.rs) | ⭐ | - | | [Day 22](https://adventofcode.com/2023/day/22) | [code](src/bin/22.rs) | | | | [Day 23](https://adventofcode.com/2023/day/23) | [code](src/bin/23.rs) | | | | [Day 24](https://adventofcode.com/2023/day/24) | [code](src/bin/24.rs) | | | diff --git a/data/examples/21.txt b/data/examples/21.txt new file mode 100644 index 0000000..c66328c --- /dev/null +++ b/data/examples/21.txt @@ -0,0 +1,11 @@ +........... +.....###.#. +.###.##..#. +..#.#...#.. +....#.#.... +.##..S####. +.##..#...#. +.......##.. +.##.#.####. +.##..##.##. +........... \ No newline at end of file diff --git a/src/bin/21.rs b/src/bin/21.rs new file mode 100644 index 0000000..964ede1 --- /dev/null +++ b/src/bin/21.rs @@ -0,0 +1,73 @@ +use std::collections::HashSet; + +advent_of_code::solution!(21); + +pub fn valid_neighbors(point: (usize, usize), map: &Vec>) -> Vec<(usize, usize)> { + let mut ret = Vec::with_capacity(4); + if point.0 > 0 && map[point.0 - 1][point.1] != '#' { + ret.push((point.0 - 1, point.1)); + } + if point.0 < map.len() - 1 && map[point.0 + 1][point.1] != '#' { + ret.push((point.0 + 1, point.1)); + } + if point.1 > 0 && map[point.0][point.1 - 1] != '#' { + ret.push((point.0, point.1 - 1)); + } + if point.1 < map[0].len() - 1 && map[point.0][point.1 + 1] != '#' { + ret.push((point.0, point.1 + 1)); + } + ret +} + +pub fn part_one(input: &str) -> Option { + let map = input + .lines() + .map(|line| line.chars().collect()) + .collect::>>(); + let start = map + .iter() + .enumerate() + .find_map(|(row, v)| { + v.iter() + .enumerate() + .find_map(|(col, c)| if *c == 'S' { Some(col) } else { None }) + .map(|col| (row, col)) + }) + .expect("There should be a starting tile."); + let final_plots = (0..64).fold( + vec![start].iter().copied().collect::>(), + |acc, _| { + let mut new_acc = HashSet::new(); + acc.iter().for_each(|point| { + let mut neighbors = valid_neighbors(*point, &map); + neighbors.drain(0..).for_each(|p| { + new_acc.insert(p); + }); + }); + new_acc + }, + ); + Some(final_plots.len() as u64) +} + +pub fn part_two(input: &str) -> Option { + // I actually have no idea how to do part 2 + input.find(|_| false) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_part_one() { + let result = part_one(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, Some(42)); + } + + #[test] + fn test_part_two() { + let result = part_two(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, None); + } +}