day10 part 2

This commit is contained in:
unintendedfraud
2023-12-19 22:29:35 +01:00
parent 3de02da73d
commit 0ac3a83e38
2 changed files with 142 additions and 38 deletions
+17 -38
View File
@@ -1,35 +1,15 @@
use std::collections::HashMap;
mod part2;
fn main() {
let pipes = HashMap::from([
('|', (0, 1)),
('-', (1, 0)),
('L', (1, 1)),
('J', (1, -1)),
('7', (1, -1)),
('F', (1, 1)),
]);
let data = util::get_data_chars("data.txt");
let (start, starting_directions) = find_starting_directions(&data, &pipes);
let (start, starting_direction) = find_starting_directions(&data);
let max = go_through_pipe(&data, start, &starting_directions);
let max = get_max_result(&data, start, &starting_direction);
let inside_count = part2::get_insides(&data, &start, &starting_direction);
println!("## part 1 max: {}", max);
}
fn go_through_pipe(
d: &Vec<Vec<char>>,
start: (i32, i32),
starting_directions: &Vec<(i32, i32)>,
) -> i32 {
let results = starting_directions
.iter()
.map(|direction| get_max_result(&d, start, direction))
.collect::<Vec<i32>>();
return *results.iter().max().unwrap();
println!("## part 2 insides: {}", inside_count);
}
fn get_max_result(d: &Vec<Vec<char>>, start: (i32, i32), direction: &(i32, i32)) -> i32 {
@@ -119,11 +99,7 @@ fn get_max_result(d: &Vec<Vec<char>>, start: (i32, i32), direction: &(i32, i32))
return count / 2;
}
fn find_starting_directions(
d: &Vec<Vec<char>>,
pipes: &HashMap<char, (i32, i32)>,
) -> ((i32, i32), Vec<(i32, i32)>) {
let mut v = Vec::new();
fn find_starting_directions(d: &Vec<Vec<char>>) -> ((i32, i32), (i32, i32)) {
let mut start: (i32, i32) = (-1, -1);
for i in 0..d.len() {
@@ -140,20 +116,23 @@ fn find_starting_directions(
panic!("failed to find the starting position");
}
// find a possible start
let possible_starts = vec![
(start.0 - 1, start.1),
(start.0, start.1 - 1),
(start.0 + 1, start.1),
(start.0, start.1 + 1),
((start.0 - 1, start.1), vec!['|', '7', 'F']),
((start.0, start.1 - 1), vec!['-', 'F', 'L']),
((start.0, start.1 + 1), vec!['-', '7', 'J']),
((start.0 + 1, start.1), vec!['|', 'J', 'L']),
];
for i in 0..possible_starts.len() {
let el = possible_starts[i];
let pos = &possible_starts[i].0;
let signs = &possible_starts[i].1;
if el.0 >= 0 && el.1 >= 0 && pipes.contains_key(&d[el.0 as usize][el.1 as usize]) {
v.push(el);
if pos.0 >= 0 && pos.1 >= 0 && signs.contains(&d[pos.0 as usize][pos.1 as usize]) {
return (start, *pos);
}
}
return (start, v);
panic!("failed to find a starting position");
}