Skip to content

Commit 34f5f8d

Browse files
committed
Refactor helper methods to the Grid struct impl.
1 parent 883600b commit 34f5f8d

File tree

2 files changed

+57
-63
lines changed

2 files changed

+57
-63
lines changed

day_12_hill_climbing_algorithm/src/grid.rs

+56-62
Original file line numberDiff line numberDiff line change
@@ -41,77 +41,71 @@ pub struct Grid {
4141
pub goal: Point,
4242
}
4343

44-
fn get_as_usize(grid: &Grid, point: &Point) -> Space {
45-
grid.spaces
46-
.get(grid.width * point.y + point.x)
47-
.unwrap()
48-
.to_owned()
49-
}
44+
impl Grid {
45+
fn idx(&self, point: &Point) -> usize {
46+
self.width * point.y + point.x
47+
}
5048

51-
fn get_as_usize_offset_row(grid: &Grid, point: &Point, y: isize) -> Space {
52-
let new_y = point.y as isize + y;
53-
grid.spaces
54-
.get(grid.width * (new_y as usize) + point.x)
55-
.unwrap()
56-
.to_owned()
57-
}
49+
fn get_by_row(&self, point: &Point, y: isize) -> &Space {
50+
let new_y = point.y as isize + y;
51+
self.spaces
52+
.get(self.width * (new_y as usize) + point.x)
53+
.unwrap()
54+
}
5855

59-
fn get_as_usize_offset_col(grid: &Grid, point: &Point, x: isize) -> Space {
60-
let new_x = point.x as isize + x;
61-
grid.spaces
62-
.get(grid.width * point.y + new_x as usize)
63-
.unwrap()
64-
.to_owned()
65-
}
56+
fn get_by_col(&self, point: &Point, x: isize) -> &Space {
57+
let new_x = point.x as isize + x;
58+
self.spaces
59+
.get(self.width * point.y + new_x as usize)
60+
.unwrap()
61+
}
6662

67-
impl Grid {
68-
69-
pub fn valid_neighbors(&self, point: &Point) -> Vec<(Point, usize)> {
70-
let mut valid = Vec::new();
71-
let cur = get_as_usize(self, point);
72-
// up
73-
if point.y >= 1 {
74-
let up = get_as_usize_offset_row(self, point, -1);
75-
if cur.can_move(&up) {
76-
valid.push(Point {
77-
x: point.x,
78-
y: point.y - 1,
79-
});
63+
pub fn valid_neighbors(&self, point: &Point) -> Vec<(Point, usize)> {
64+
let mut valid = Vec::new();
65+
let cur = self.spaces.get(self.idx(point)).unwrap();
66+
// up
67+
if point.y >= 1 {
68+
let up = self.get_by_row(point, -1);
69+
if cur.can_move(&up) {
70+
valid.push(Point {
71+
x: point.x,
72+
y: point.y - 1,
73+
});
74+
}
8075
}
81-
}
8276

83-
// right
84-
if point.x + 1 < self.width {
85-
let right = get_as_usize_offset_col(self, point, 1);
86-
if cur.can_move(&right) {
87-
valid.push(Point {
88-
x: point.x + 1,
89-
y: point.y,
90-
});
77+
// right
78+
if point.x + 1 < self.width {
79+
let right = self.get_by_col(point, 1);
80+
if cur.can_move(&right) {
81+
valid.push(Point {
82+
x: point.x + 1,
83+
y: point.y,
84+
});
85+
}
9186
}
92-
}
9387

94-
// down
95-
if point.y + 1 < self.height {
96-
let down = get_as_usize_offset_row(self, point, 1);
97-
if cur.can_move( &down) {
98-
valid.push(Point {
99-
x: point.x,
100-
y: point.y + 1,
101-
});
88+
// down
89+
if point.y + 1 < self.height {
90+
let down = self.get_by_row(point, 1);
91+
if cur.can_move(&down) {
92+
valid.push(Point {
93+
x: point.x,
94+
y: point.y + 1,
95+
});
96+
}
10297
}
103-
}
10498

105-
// left
106-
if point.x >= 1 {
107-
let left = get_as_usize_offset_col(self, point, -1);
108-
if cur.can_move( &left) {
109-
valid.push(Point {
110-
x: point.x - 1,
111-
y: point.y,
112-
});
99+
// left
100+
if point.x >= 1 {
101+
let left = self.get_by_col(point, -1);
102+
if cur.can_move(&left) {
103+
valid.push(Point {
104+
x: point.x - 1,
105+
y: point.y,
106+
});
107+
}
113108
}
109+
valid.into_iter().map(|p| (p, 1)).collect()
114110
}
115-
valid.into_iter().map(|p| (p, 1)).collect()
116-
}
117111
}

day_12_hill_climbing_algorithm/src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{fs::read_to_string, io::Error};
33
use pathfinding::dijkstra;
44

55
mod grid;
6-
use grid::{Grid, Space};
6+
use grid::Space;
77

88
mod parser;
99
use parser::parse;

0 commit comments

Comments
 (0)