From 3522b38394bdf271a0f8208572082956b1ea7c5c Mon Sep 17 00:00:00 2001 From: Bert Peters Date: Fri, 10 Nov 2023 08:47:39 +0100 Subject: [PATCH] Try to normalize the cube a little --- 2022/src/day22.rs | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/2022/src/day22.rs b/2022/src/day22.rs index eae6c34..fe19522 100644 --- a/2022/src/day22.rs +++ b/2022/src/day22.rs @@ -128,14 +128,18 @@ fn parse_map(input: &[u8]) -> IResult<&[u8], (Map, Vec)> { )(input) } +fn find_starting_x(top_row: &[u8]) -> Result { + top_row + .iter() + .position(|&b| b == b'.') + .context("Could not find starting position") +} + pub fn part1(input: &[u8]) -> Result { let (map, steps) = parse_input(input, parse_map)?; let mut dir = Direction::Right; let mut y = 0; - let mut x = map[y] - .iter() - .position(|&b| b == b'.') - .context("Could not find starting position")?; + let mut x = find_starting_x(&map[y])?; for step in steps { match step { @@ -268,13 +272,29 @@ fn break_squares<'a>(map: &[&'a [u8]], side_length: usize) -> [(Map<'a>, usize, } pub fn part2(input: &[u8]) -> Result { - let (map, _steps) = parse_input(input, parse_map)?; + let (map, steps) = parse_input(input, parse_map)?; let side_length = side_length_of(&map); - let _squares = break_squares(&map, side_length); + let squares = break_squares(&map, side_length); - anyhow::bail!("not implemented") + let mut current_square = 0; + let mut y = 0; + let mut x = find_starting_x(&squares[current_square].0[y])?; + let mut dir = Direction::Right; + + for step in steps { + match step { + Step::Left => dir = dir.turn_left(), + Step::Right => dir = dir.turn_right(), + Step::Forward(amount) => anyhow::bail!("not implemented"), + } + } + + let real_x = x + squares[current_square].1 * side_length; + let real_y = y + squares[current_square].2 * side_length; + + Ok((1000 * (real_y + 1) + 4 * (real_x + 1) + dir as usize).to_string()) } #[cfg(test)]