Skip to content

Commit

Permalink
day21: Initial solution for part1
Browse files Browse the repository at this point in the history
  • Loading branch information
pedantic79 committed Jan 5, 2024
1 parent c45e95e commit 67ee368
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
- [solution](src/day19.rs)
- [Day 20: Pulse Propagation](https://adventofcode.com/2023/day/20)
- [solution](src/day20.rs)
- [Day 21: Step Counter](https://adventofcode.com/2023/day/21)
- [solution](src/day21.rs)
<!-- Insert before -->

See:
Expand Down
2 changes: 1 addition & 1 deletion input
Submodule input updated from acb72c to f9b1cf
176 changes: 176 additions & 0 deletions src/day21.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
use std::{fmt::Debug, iter::repeat};

use aoc_runner_derive::{aoc, aoc_generator};

#[derive(PartialEq, Eq)]
pub enum State {
Rock,
Plot,
Step,
}

impl Debug for State {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Rock => write!(f, "#"),
Self::Plot => write!(f, "."),
Self::Step => write!(f, "O"),
}
}
}

#[derive(PartialEq, Eq)]
pub struct Grid {
grid: Vec<State>,
width: usize,
height: usize,
start: (usize, usize),
}

impl Grid {
pub fn display(&self, locations: &[bool]) {
let mut idx = 0;
for (c, plots) in self.grid.iter().zip(locations.iter().chain(repeat(&false))) {
if *plots {
print!("O");
} else {
print!("{:?}", c);
}
idx += 1;
if idx == self.width {
println!();
idx = 0;
}
}
}

fn neighbors(&self, index: usize) -> [usize; 4] {
[
index + 1,
index.saturating_sub(1),
index + self.width,
index.saturating_sub(self.width),
]
}

fn step(&self, locations: &[bool]) -> Vec<bool> {
let mut res = vec![false; locations.len()];

for (index, &loc) in locations.iter().enumerate() {
if loc {
for neigh in self.neighbors(index) {
if let Some(state) = self.grid.get(neigh) {
if state == &State::Plot {
res[neigh] = true;
}
}
}
}
}

res
}
}

#[aoc_generator(day21)]
pub fn generator(input: &str) -> Grid {
let mut start = (0, 0);
let mut height = 0;
let mut width = 0;
let mut grid = Vec::new();

for l in input.lines() {
width = l.len();

grid.extend(l.bytes().enumerate().map(|(cidx, b)| match b {
b'#' => State::Rock,
b'.' => State::Plot,
b'S' => {
start = (height, cidx);
State::Plot
}
_ => panic!("unexpected byte"),
}));
height += 1;
}

Grid {
grid,
height,
width,
start,
}
}

fn solve<const STEPS: usize>(inputs: &Grid) -> usize {
let mut taken = vec![false; inputs.grid.len()];
taken[inputs.start.0 * inputs.width + inputs.start.1] = true;

for _ in 0..STEPS {
taken = inputs.step(&taken);
}

taken.iter().filter(|x| **x).count()
}

#[aoc(day21, part1)]
pub fn part1(inputs: &Grid) -> usize {
solve::<64>(inputs)
}

// #[aoc(day21, part2)]
pub fn part2(inputs: &Grid) -> usize {
unimplemented!()
}

#[cfg(test)]
mod tests {
use super::*;

const SAMPLE: &str = r"...........
.....###.#.
.###.##..#.
..#.#...#..
....#.#....
.##..S####.
.##..#...#.
.......##..
.##.#.####.
.##..##.##.
...........";

#[test]
pub fn input_test() {
// println!("{:?}", generator(SAMPLE));
let grid = generator(SAMPLE);
grid.display(&[]);

// assert_eq!(generator(SAMPLE), Object());
}

#[test]
pub fn part1_test() {
assert_eq!(solve::<6>(&generator(SAMPLE)), 16);
}

#[test]
pub fn part2_test() {
// assert_eq!(part2(&generator(SAMPLE)), 336);
}

mod regression {
use super::*;

const INPUT: &str = include_str!("../input/2023/day21.txt");
const ANSWERS: (usize, usize) = (3764, 0);

#[test]
pub fn test() {
let input = INPUT.trim_end_matches('\n');
let output = generator(input);

assert_eq!(part1(&output), ANSWERS.0);
// assert_eq!(part2(&output), ANSWERS.1);
}
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ pub mod day17;
pub mod day18;
pub mod day19;
pub mod day20;
pub mod day21;
// Insert before
aoc_lib! { year = 2023 }

0 comments on commit 67ee368

Please sign in to comment.