A little more progress on day 12 part 1

This commit is contained in:
Timothy Warren 2022-12-16 16:05:52 -05:00
parent 5346caaf26
commit 6941e0024c
3 changed files with 76 additions and 0 deletions

7
aoc-shared/src/enums.rs Normal file
View File

@ -0,0 +1,7 @@
#[derive(Debug, PartialEq, Copy, Clone)]
pub enum Direction {
Up,
Down,
Left,
Right,
}

View File

@ -1,4 +1,6 @@
pub mod grid;
pub mod enums;
pub use grid::*;
#[macro_export]

View File

@ -1,5 +1,64 @@
use aoc_shared::enums::Direction;
use aoc_shared::grid::Grid as BaseGrid;
use aoc_shared::grid::Grid2d;
use aoc_shared::impl_grid_newtype;
#[derive(Debug)]
struct Grid<T>(BaseGrid<T>);
impl_grid_newtype!(Grid, BaseGrid<char>, char);
impl Grid<char> {
pub fn from_file_str(file_str: &str) -> Self {
let lines: Vec<&str> = file_str.lines().collect();
let width = lines[0].len();
let mut grid = Grid::new(width);
lines
.into_iter()
.map(|line| line.chars())
.for_each(|line_chars| grid.vec.append(&mut line_chars.collect::<Vec<char>>()));
grid
}
fn find_pos(&self, value: char) -> Option<usize> {
self.vec.iter().position(|item| *item == value)
}
fn find_start_and_end(&self) -> (usize, usize) {
let start = self.vec.iter().position(|item| *item == 'S').unwrap();
let end = self.vec.iter().position(|item| *item == 'E').unwrap();
(start, end)
}
}
// ----------------------------------------------------------------------------
#[derive(Debug)]
struct Pathfinder {
grid: Grid<char>,
steps: Vec<Direction>,
}
impl Pathfinder {
pub fn from_file_str(file_str: &str) -> Self {
Pathfinder {
grid: Grid::from_file_str(file_str),
steps: Vec::new(),
}
}
fn find_start_and_end(&self) -> (usize, usize) {
(self.grid.find_pos('S').unwrap(), self.grid.find_pos('E').unwrap())
}
}
// ----------------------------------------------------------------------------
fn main() {
let file_str = include_str!("input.txt");
let mut finder = Pathfinder::from_file_str(file_str);
}
#[cfg(test)]
@ -9,4 +68,12 @@ mod tests {
fn get_test_data() -> &'static str {
include_str!("test-input.txt")
}
#[test]
fn get_start_end() {
let finder = Pathfinder::from_file_str(get_test_data());
let (start, end) = finder.find_start_and_end();
assert_eq!((start, end), (0, 21));
}
}