Skip to content

Commit

Permalink
feat: filter squares by elevation
Browse files Browse the repository at this point in the history
  • Loading branch information
saiteki-kai committed Aug 5, 2024
1 parent 615d9b3 commit 823667c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
13 changes: 7 additions & 6 deletions 2022/src/days/day12/heatmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl HeatMap {
self.squares.get(index)
}

pub fn neighbour(&self, square: &Square) -> Vec<Square> {
pub fn neighbours(&self, square: &Square) -> Vec<Square> {
let positions: Vec<(usize, usize)> = vec![
(Some(square.row), square.col.checked_sub(1)),
(Some(square.row), square.col.checked_add(1)),
Expand All @@ -49,7 +49,6 @@ impl HeatMap {
]
.into_iter()
.filter_map(|(a, b)| {
println!("{:#?} {:#?}", a, b);
if let (Some(a), Some(b)) = (a, b) {
Some((a, b))
} else {
Expand All @@ -66,12 +65,14 @@ impl HeatMap {
}

pub fn possible_destinations(&self, square: &Square) -> Vec<Square> {
self.neighbour(square)
self.neighbours(square)
.iter()
.filter(|s| {
(s.get_elevation().expect("bad value") as i8
- square.get_elevation().expect("bad value") as i8)
<= 1
let level_diff = s.get_elevation().expect("bad value") as i8
- square.get_elevation().expect("bad value") as i8;

// next level || same level
level_diff == 1 || level_diff == 0
})
.cloned()
.collect()
Expand Down
12 changes: 11 additions & 1 deletion 2022/src/days/day12/part1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,16 @@ fn random_path(heatmap: &HeatMap) {

println!("{}", start.value);

let mut counter = 0;

while !current_square.is_end() {
let dests = heatmap.possible_destinations(&current_square);
println!("{} possible destinations", dests.len());

for d in &dests {
println!("{} -> {}", current_square.value, d.value);
}

print!("{}: ", current_square.value);
if current_square.value == 'd' {
println!("entered!");
Expand All @@ -42,7 +48,11 @@ fn random_path(heatmap: &HeatMap) {
.choose_weighted(&mut rand::thread_rng(), |d| d.get_elevation().unwrap_or(0))
.unwrap(),
);

counter += 1;
}

println!("{} moves", counter);
}

#[cfg(test)]
Expand Down Expand Up @@ -85,7 +95,7 @@ mod tests {
assert_eq!(start.row, 0);
assert_eq!(start.col, 0);

assert_eq!(heatmap.neighbour(start).len(), 2);
assert_eq!(heatmap.neighbours(start).len(), 2);
assert_eq!(heatmap.possible_destinations(start).len(), 2);
}

Expand Down

0 comments on commit 823667c

Please sign in to comment.