From 94a5b006aacdf7bf98449166bed1694daef90831 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Wed, 5 Jul 2023 21:01:34 +0200 Subject: [PATCH] day(22): use proper ranges instead of butchering vectors Signed-off-by: Matej Focko --- src/bin/day22.rs | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/src/bin/day22.rs b/src/bin/day22.rs index 4b92809..5854926 100644 --- a/src/bin/day22.rs +++ b/src/bin/day22.rs @@ -1,4 +1,4 @@ -use std::{collections::HashMap, hash::Hash}; +use std::{collections::HashMap, hash::Hash, ops::Range}; use aoc_2022::*; @@ -7,7 +7,6 @@ type Output = isize; type Position = Vector2D; type Direction = Vector2D; -type Boundary = Vector2D; enum Instruction { Move(usize), @@ -23,12 +22,12 @@ enum Orientation { struct MonkeyMap { map: Vec>, - boundaries: HashMap, + boundaries: HashMap>, instructions: Vec, } -fn wrapping_step(position: usize, diff: isize, range: Boundary) -> usize { - let (lower, upper) = (range.x(), range.y()); +fn wrapping_step(position: usize, diff: isize, range: &Range) -> usize { + let (lower, upper) = (range.start, range.end); let range_size = (upper - lower) as isize; (lower as isize + (position as isize + diff - lower as isize + range_size) % range_size) @@ -42,8 +41,8 @@ trait Wrap { struct Wrap2D; impl Wrap for Wrap2D { fn next(&self, state: &State<'_>) -> (Position, Direction) { - let h_bound = state.input.boundaries[&Orientation::Horizontal(state.position.y())]; - let v_bound = state.input.boundaries[&Orientation::Vertical(state.position.x())]; + let h_bound = &state.input.boundaries[&Orientation::Horizontal(state.position.y())]; + let v_bound = &state.input.boundaries[&Orientation::Vertical(state.position.x())]; ( Position::new( @@ -56,8 +55,8 @@ impl Wrap for Wrap2D { } struct Face { - horizontal_bound: Boundary, - vertical_bound: Boundary, + horizontal_bound: Range, + vertical_bound: Range, } struct Wrap3D { @@ -80,9 +79,13 @@ impl Wrap for Wrap3D { wrapping_step( state.position.x(), state.direction.x(), - face.horizontal_bound, + &face.horizontal_bound, + ), + wrapping_step( + state.position.y(), + state.direction.y(), + &face.vertical_bound, ), - wrapping_step(state.position.y(), state.direction.y(), face.vertical_bound), ), state.direction, ) @@ -99,7 +102,7 @@ impl<'a> State<'a> { fn new(input: &'a Input) -> State<'a> { Self { input, - position: Position::new(input.boundaries[&Orientation::Horizontal(0)].x(), 0), + position: Position::new(input.boundaries[&Orientation::Horizontal(0)].start, 0), direction: Direction::new(1, 0), } } @@ -173,7 +176,7 @@ impl Solution for Day22 { let mut last_non_empty = first_non_empty.skip_while(|&(_, &c)| c != ' '); let end = last_non_empty.next().unwrap_or((map[row].len(), &'_')).0; - boundaries.insert(Orientation::Horizontal(row), Boundary::new(start, end)); + boundaries.insert(Orientation::Horizontal(row), start..end); } for col in 0..map[0].len() { @@ -185,7 +188,7 @@ impl Solution for Day22 { let mut last_non_empty = first_non_empty.skip_while(|&(_, &c)| c != ' '); let end = last_non_empty.next().unwrap_or((map.len(), &'_')).0; - boundaries.insert(Orientation::Vertical(col), Boundary::new(start, end)); + boundaries.insert(Orientation::Vertical(col), start..end); } let unparsed_instructions_str = instructions.trim_end();