Skip to content

Commit

Permalink
Day 21... tapped out on part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
BakerNet committed Dec 21, 2023
1 parent e9b1d25 commit cfa268a
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) | | |
Expand Down
11 changes: 11 additions & 0 deletions data/examples/21.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
...........
.....###.#.
.###.##..#.
..#.#...#..
....#.#....
.##..S####.
.##..#...#.
.......##..
.##.#.####.
.##..##.##.
...........
73 changes: 73 additions & 0 deletions src/bin/21.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use std::collections::HashSet;

advent_of_code::solution!(21);

pub fn valid_neighbors(point: (usize, usize), map: &Vec<Vec<char>>) -> 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<u64> {
let map = input
.lines()
.map(|line| line.chars().collect())
.collect::<Vec<Vec<_>>>();
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::<HashSet<_>>(),
|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<usize> {
// 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);
}
}

0 comments on commit cfa268a

Please sign in to comment.