Skip to content

Commit

Permalink
[2023] Day 21 part a
Browse files Browse the repository at this point in the history
  • Loading branch information
connorslade committed Dec 21, 2023
1 parent c7dc7a4 commit 7b2a72b
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Thank you to [Eric Wastl](http://was.tl) for running this incredible yearly even
- [Day 18: Lavaduct Lagoon](aoc_2023/src/day_18.rs)
- [Day 19: Aplenty](aoc_2023/src/day_19.rs)
- [Day 20: Pulse Propagation](aoc_2023/src/day_20.rs)
- [Day 21: Step Counter](aoc_2023/src/day_21.rs)
<!-- MARKER -->

## [2022](https://adventofcode.com/2022) [![aoc_2022](https://github.com/Basicprogrammer10/advent-of-code/actions/workflows/aoc_2022.yml/badge.svg)](https://github.com/Basicprogrammer10/advent-of-code/actions/workflows/aoc_2022.yml)
Expand Down
14 changes: 12 additions & 2 deletions aoc_2023/src/aoc_lib/matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ impl<T> Matrix<T> {
}

pub fn get(&self, pos: Vec2<usize>) -> Option<&T> {
(pos.x() >= self.size.x() || pos.y() >= self.size.y())
(pos.x() < self.size.x() && pos.y() < self.size.y())
.then(|| &self.data[pos.y() * self.size.x() + pos.x()])
}

pub fn get_mut(&mut self, pos: Vec2<usize>) -> Option<&mut T> {
(pos.x() >= self.size.x() || pos.y() >= self.size.y())
(pos.x() < self.size.x() && pos.y() < self.size.y())
.then(|| &mut self.data[pos.y() * self.size.x() + pos.x()])
}

Expand All @@ -61,6 +61,16 @@ impl<T> Matrix<T> {
pub fn size(&self) -> Vec2<usize> {
self.size
}

pub fn find(&self, value: T) -> Option<Vec2<usize>>
where
T: PartialEq,
{
self.data
.iter()
.position(|x| x == &value)
.map(|x| vector!(x % self.size.x(), x / self.size.x()))
}
}

impl<T> Index<[usize; 2]> for Matrix<T> {
Expand Down
135 changes: 135 additions & 0 deletions aoc_2023/src/day_21.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
use std::collections::HashSet;

use common::{Answer, Solution};
use nd_vec::vector;

use crate::aoc_lib::{direction::Direction, matrix::Matrix};

pub struct Day21;

impl Solution for Day21 {
fn name(&self) -> &'static str {
"Step Counter"
}

fn part_a(&self, input: &str) -> Answer {
let map = parse(input);
let mut pos = HashSet::new();
pos.insert(map.find(Tile::Start).unwrap().num_cast::<i32>().unwrap());

for _ in 0..64 {
let mut new_pos = HashSet::new();

for p in pos {
for dir in Direction::ALL {
let new_p = dir.advance(p);
if !map.contains(new_p)
|| *map.get(new_p.num_cast().unwrap()).unwrap() != Tile::Wall
{
new_pos.insert(new_p);
}
}
}

pos = new_pos;
}

pos.len().into()
}

fn part_b(&self, input: &str) -> Answer {
let map = parse(input);

let size = map.size.num_cast::<i32>().unwrap();
let (sx, sy) = (size.x(), size.y());

let mut pos = HashSet::new();
pos.insert(map.find(Tile::Start).unwrap().num_cast::<i32>().unwrap());

for i in 0..500 {
println!("{:.1}% done", i as f32 / 500.0 * 100.0);

let mut new_pos = HashSet::new();

for p in pos {
for dir in Direction::ALL {
let new_p = dir.advance(p);
if {
let mut pos = new_p;

if pos.x() < 0 {
pos = vector!(sx + pos.x() % sx, pos.y());
}

if pos.y() < 0 {
pos = vector!(pos.x(), sy + pos.y() % sy);
}

pos = vector!(pos.x() % sx, pos.y());
pos = vector!(pos.x(), pos.y() % sy);

let pos = pos.num_cast::<usize>().unwrap();
*map.get(pos).unwrap()
} != Tile::Wall
{
new_pos.insert(new_p);
}
}
}

pos = new_pos;
}

// pos.len().into()

Answer::Unimplemented
}
}

#[derive(Debug, PartialEq, Eq, Copy, Clone)]
enum Tile {
Garden,
Wall,
Start,
}

fn parse(input: &str) -> Matrix<Tile> {
Matrix::new_chars(input, |x| match x {
'#' => Tile::Wall,
'.' => Tile::Garden,
'S' => Tile::Start,
_ => panic!("Invalid input"),
})
}

#[cfg(test)]
mod test {
use common::Solution;
use indoc::indoc;

use super::Day21;

const CASE: &str = indoc! {"
...........
.....###.#.
.###.##..#.
..#.#...#..
....#.#....
.##..S####.
.##..#...#.
.......##..
.##.#.####.
.##..##.##.
...........
"};

#[test]
fn part_a() {
assert_eq!(Day21.part_a(CASE), 4056.into());
}

#[test]
fn part_b() {
assert_eq!(Day21.part_b(CASE), ().into());
}
}
2 changes: 2 additions & 0 deletions aoc_2023/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ mod day_17;
mod day_18;
mod day_19;
mod day_20;
mod day_21;
// [import_marker]

#[rustfmt::skip]
Expand All @@ -46,5 +47,6 @@ pub const ALL: &[&dyn Solution] = &[
&day_18::Day18,
&day_19::Day19,
&day_20::Day20,
&day_21::Day21,
// [list_marker]
];

0 comments on commit 7b2a72b

Please sign in to comment.