From 0b21e94cf7a84bf9b02a27b8eee8a8a5206e680c Mon Sep 17 00:00:00 2001 From: Justin Wernick Date: Sun, 17 Dec 2023 09:12:54 +0200 Subject: Start of day 17, playing with nalgebra --- 2023/src/bin/day_17.rs | 63 ++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 56 insertions(+), 7 deletions(-) (limited to '2023/src/bin') 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> { - 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); + +#[derive(Debug)] +struct Position { + pos: Point2, + facing: Unit>, + 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::() + })), + ), + |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!() } } -- cgit v1.2.3