diff options
author | Justin Wernick <justin@worthe-it.co.za> | 2023-12-17 09:12:54 +0200 |
---|---|---|
committer | Justin Wernick <justin@worthe-it.co.za> | 2023-12-17 09:12:54 +0200 |
commit | 0b21e94cf7a84bf9b02a27b8eee8a8a5206e680c (patch) | |
tree | 6eaa66983e26304d1cff7b9a969009beb8df8d8a /2023/src | |
parent | 00d27a850b47a7127f5e72a16e8d31333fc734a9 (diff) |
Start of day 17, playing with nalgebra
Diffstat (limited to '2023/src')
-rw-r--r-- | 2023/src/bin/day_17.rs | 63 |
1 files changed, 56 insertions, 7 deletions
diff --git a/2023/src/bin/day_17.rs b/2023/src/bin/day_17.rs index b3a610b..9cd6d76 100644 --- a/2023/src/bin/day_17.rs +++ b/2023/src/bin/day_17.rs @@ -1,19 +1,68 @@ -use nom::IResult; +use nalgebra::{DMatrix, Point2, Unit, Vector2}; +use nom::{ + character::complete::{line_ending, satisfy}, + combinator::{map, map_res}, + multi::{many1, separated_list1}, + IResult, +}; use std::fs; fn main() -> Result<(), Box<dyn std::error::Error>> { - let input = fs::read_to_string("inputs/day_2.txt")?; - let parsed = Example::parser(&input).unwrap().1; - dbg!(&parsed); + let input = fs::read_to_string("inputs/day_17.txt")?; + let parsed = HeatlossMap::parser(&input).unwrap().1; + dbg!(&parsed.find_shortest_heatloss_path()); Ok(()) } #[derive(Debug)] -struct Example; +struct HeatlossMap(DMatrix<u32>); + +#[derive(Debug)] +struct Position { + pos: Point2<usize>, + facing: Unit<Vector2<usize>>, + duration_with_facing: usize, +} + +impl HeatlossMap { + fn parser(input: &str) -> IResult<&str, Self> { + map( + separated_list1( + line_ending, + many1(map_res(satisfy(|c| c.is_digit(10)), |d| { + d.to_string().parse::<u32>() + })), + ), + |digits_vec| { + HeatlossMap(DMatrix::from_iterator( + digits_vec.len(), + digits_vec[0].len(), + digits_vec.into_iter().flat_map(|row| row.into_iter()), + )) + }, + )(input) + } + + fn find_shortest_heatloss_path(&self) -> usize { + let start = Position { + pos: Point2::new(0, 0), + facing: Vector2::x_axis(), + duration_with_facing: 0, + }; + + dbg!(&start); + dbg!(self.0.get((start.pos.x, start.pos.y))); + + let moved_pos = start.pos + *start.facing; + dbg!(&moved_pos); + dbg!(self.0.get((moved_pos.x, moved_pos.y))); + // start is 0,0 + // end is max,max + // can't go more than 3 in a straight line + // can only turn left or right, not go backwards + // -impl Example { - fn parser(_input: &str) -> IResult<&str, Self> { todo!() } } |