day_10: basics done but counting all the possible trails
This commit is contained in:
8
assets/day_10_trail_map_input
Normal file
8
assets/day_10_trail_map_input
Normal file
@@ -0,0 +1,8 @@
|
||||
89010123
|
||||
78121874
|
||||
87430965
|
||||
96549874
|
||||
45678903
|
||||
32019012
|
||||
01329801
|
||||
10456732
|
||||
@@ -78,6 +78,6 @@ pub fn historian_hysteria() {
|
||||
// println!("The disk checksum is {}", disk_checksum);
|
||||
|
||||
// Tuesday 10
|
||||
let trailhead_score = tue_10::hoof_it(".");
|
||||
let trailhead_score = tue_10::hoof_it("./assets/day_10_trail_map_input");
|
||||
println!("The trail head sum score is {}", trailhead_score);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,89 @@
|
||||
use utils::read_trail_map_input;
|
||||
|
||||
use super::*;
|
||||
|
||||
pub fn hoof_it(input: &str) -> usize {
|
||||
0
|
||||
let trail_map = read_trail_map_input(input);
|
||||
|
||||
let vector_of_scores = explore_map(trail_map);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
sum_of_scores
|
||||
}
|
||||
|
||||
pub fn explore_map(map: TrailMap) -> Vec<TrailScore> {
|
||||
let mut trail_score_list: Vec<TrailScore> = vec![];
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
trail_score_list
|
||||
}
|
||||
|
||||
pub fn explore_position(x: TrailPosition, y: TrailPosition, map: &TrailMap) -> usize {
|
||||
if map[y][x] == 9 {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
score_sum
|
||||
}
|
||||
|
||||
pub fn get_pos_surroundings(
|
||||
x: TrailPosition,
|
||||
y: TrailPosition,
|
||||
map: &TrailMap,
|
||||
) -> Vec<(usize, usize)> {
|
||||
let mut trail_pos_vec: Vec<(usize, usize)> = vec![];
|
||||
|
||||
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 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, y - 1));
|
||||
}
|
||||
|
||||
if y < map.len() - 1 {
|
||||
trail_pos_vec.push((x, y + 1));
|
||||
}
|
||||
|
||||
trail_pos_vec
|
||||
}
|
||||
|
||||
@@ -79,3 +79,5 @@ pub type DiskChecksum = usize;
|
||||
|
||||
// Hoof it
|
||||
pub type TrailScore = usize;
|
||||
pub type TrailPosition = usize;
|
||||
pub type TrailMap = Vec<Vec<TrailPosition>>;
|
||||
|
||||
@@ -189,6 +189,23 @@ pub fn read_disk_map_input(input: &str) -> DiskMap {
|
||||
disk_map
|
||||
}
|
||||
|
||||
pub fn read_trail_map_input(input: &str) -> TrailMap {
|
||||
let mut trail_map: TrailMap = vec![];
|
||||
|
||||
for line in read_to_string(input).unwrap().lines() {
|
||||
let mut trail_row: Vec<usize> = vec![];
|
||||
for position in line.chars() {
|
||||
if let Some(digit) = position.to_digit(10) {
|
||||
trail_row.push(digit as usize);
|
||||
}
|
||||
}
|
||||
|
||||
trail_map.push(trail_row);
|
||||
}
|
||||
|
||||
trail_map
|
||||
}
|
||||
|
||||
pub fn calc_distance<T>(num_1: T, num_2: T) -> T
|
||||
where
|
||||
T: PartialOrd + Sub<Output = T>,
|
||||
|
||||
Reference in New Issue
Block a user