day_10: part 1 done

This commit is contained in:
2024-12-15 12:53:05 +01:00
parent e896429b99
commit 39d61cb91d
3 changed files with 71 additions and 27 deletions

View File

@@ -9,7 +9,6 @@ pub fn hoof_it(input: &str) -> usize {
let mut sum_of_scores: usize = 0;
println!("The score list is: {:?}", vector_of_scores);
// TODO: improve, this is dirty
for score in vector_of_scores {
sum_of_scores += score;
@@ -24,7 +23,8 @@ pub fn explore_map(map: TrailMap) -> Vec<TrailScore> {
for y_index in 0..map.len() {
for x_index in 0..map[y_index].len() {
if map[y_index][x_index] == 0 {
trail_score_list.push(explore_position(x_index, y_index, &map));
let mut reached_ends: Vec<(usize, usize)> = vec![];
trail_score_list.push(explore_position(x_index, y_index, &map, &mut reached_ends));
}
}
}
@@ -32,15 +32,23 @@ pub fn explore_map(map: TrailMap) -> Vec<TrailScore> {
trail_score_list
}
pub fn explore_position(x: TrailPosition, y: TrailPosition, map: &TrailMap) -> usize {
pub fn explore_position(
x: TrailPosition,
y: TrailPosition,
map: &TrailMap,
reached_ends: &mut Vec<(usize, usize)>,
) -> usize {
if map[y][x] == 9 {
reached_ends.push((x, y));
return 1;
}
let mut score_sum: usize = 0;
for next_position in get_pos_surroundings(x, y, map) {
if map[next_position.1][next_position.0] == map[y][x] + 1 {
score_sum += explore_position(next_position.0, next_position.1, map);
if map[next_position.1][next_position.0] == map[y][x] + 1
&& !reached_ends.contains(&(next_position.0, next_position.1))
{
score_sum += explore_position(next_position.0, next_position.1, map, reached_ends);
}
}
@@ -57,24 +65,24 @@ pub fn get_pos_surroundings(
if x > 0 {
trail_pos_vec.push((x - 1, y));
if y > 0 {
trail_pos_vec.push((x - 1, y - 1));
}
if y < map.len() - 1 {
trail_pos_vec.push((x - 1, y + 1));
}
// if y > 0 {
// trail_pos_vec.push((x - 1, y - 1));
// }
// if y < map.len() - 1 {
// trail_pos_vec.push((x - 1, y + 1));
// }
}
if x < map[0].len() - 1 {
trail_pos_vec.push((x + 1, y));
if y > 0 {
trail_pos_vec.push((x + 1, y - 1));
}
if y < map.len() - 1 {
trail_pos_vec.push((x + 1, y + 1));
}
// if y > 0 {
// trail_pos_vec.push((x + 1, y - 1));
// }
//
// if y < map.len() - 1 {
// trail_pos_vec.push((x + 1, y + 1));
// }
}
if y > 0 {