1
0
Fork 0

day(22): use proper ranges instead of butchering vectors

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2023-07-05 21:01:34 +02:00
parent f237ebc936
commit 94a5b006aa
Signed by: mfocko
GPG key ID: 7C47D46246790496

View file

@ -1,4 +1,4 @@
use std::{collections::HashMap, hash::Hash}; use std::{collections::HashMap, hash::Hash, ops::Range};
use aoc_2022::*; use aoc_2022::*;
@ -7,7 +7,6 @@ type Output = isize;
type Position = Vector2D<usize>; type Position = Vector2D<usize>;
type Direction = Vector2D<isize>; type Direction = Vector2D<isize>;
type Boundary = Vector2D<usize>;
enum Instruction { enum Instruction {
Move(usize), Move(usize),
@ -23,12 +22,12 @@ enum Orientation {
struct MonkeyMap { struct MonkeyMap {
map: Vec<Vec<char>>, map: Vec<Vec<char>>,
boundaries: HashMap<Orientation, Boundary>, boundaries: HashMap<Orientation, Range<usize>>,
instructions: Vec<Instruction>, instructions: Vec<Instruction>,
} }
fn wrapping_step(position: usize, diff: isize, range: Boundary) -> usize { fn wrapping_step(position: usize, diff: isize, range: &Range<usize>) -> usize {
let (lower, upper) = (range.x(), range.y()); let (lower, upper) = (range.start, range.end);
let range_size = (upper - lower) as isize; let range_size = (upper - lower) as isize;
(lower as isize + (position as isize + diff - lower as isize + range_size) % range_size) (lower as isize + (position as isize + diff - lower as isize + range_size) % range_size)
@ -42,8 +41,8 @@ trait Wrap {
struct Wrap2D; struct Wrap2D;
impl Wrap for Wrap2D { impl Wrap for Wrap2D {
fn next(&self, state: &State<'_>) -> (Position, Direction) { fn next(&self, state: &State<'_>) -> (Position, Direction) {
let h_bound = state.input.boundaries[&Orientation::Horizontal(state.position.y())]; let h_bound = &state.input.boundaries[&Orientation::Horizontal(state.position.y())];
let v_bound = state.input.boundaries[&Orientation::Vertical(state.position.x())]; let v_bound = &state.input.boundaries[&Orientation::Vertical(state.position.x())];
( (
Position::new( Position::new(
@ -56,8 +55,8 @@ impl Wrap for Wrap2D {
} }
struct Face { struct Face {
horizontal_bound: Boundary, horizontal_bound: Range<usize>,
vertical_bound: Boundary, vertical_bound: Range<usize>,
} }
struct Wrap3D { struct Wrap3D {
@ -80,9 +79,13 @@ impl Wrap for Wrap3D {
wrapping_step( wrapping_step(
state.position.x(), state.position.x(),
state.direction.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, state.direction,
) )
@ -99,7 +102,7 @@ impl<'a> State<'a> {
fn new(input: &'a Input) -> State<'a> { fn new(input: &'a Input) -> State<'a> {
Self { Self {
input, 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), direction: Direction::new(1, 0),
} }
} }
@ -173,7 +176,7 @@ impl Solution<Input, Output> for Day22 {
let mut last_non_empty = first_non_empty.skip_while(|&(_, &c)| c != ' '); let mut last_non_empty = first_non_empty.skip_while(|&(_, &c)| c != ' ');
let end = last_non_empty.next().unwrap_or((map[row].len(), &'_')).0; 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() { for col in 0..map[0].len() {
@ -185,7 +188,7 @@ impl Solution<Input, Output> for Day22 {
let mut last_non_empty = first_non_empty.skip_while(|&(_, &c)| c != ' '); let mut last_non_empty = first_non_empty.skip_while(|&(_, &c)| c != ' ');
let end = last_non_empty.next().unwrap_or((map.len(), &'_')).0; 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(); let unparsed_instructions_str = instructions.trim_end();