Compare commits

..

3 Commits

Author SHA1 Message Date
Mathis 1fa974ebc5 Ignore .vscode 2023-12-25 01:02:50 +01:00
Mathis 498151008f 2023-17: Add down dir to start cost 2023-12-25 01:02:36 +01:00
Mathis 1f901849d6 2023-17: Start implementing part one 2023-12-25 00:46:35 +01:00
6 changed files with 160 additions and 0 deletions

2
.gitignore vendored
View File

@ -16,3 +16,5 @@ Cargo.lock
# Added by cargo
/target
.vscode

View File

@ -0,0 +1,13 @@
2413432311323
3215453535623
3255245654254
3446585845452
4546657867536
1438598798454
4457876987766
3637877979653
4654967986887
4564679986453
1224686865563
2546548887735
4322674655533

View File

@ -0,0 +1,7 @@
#[derive(Debug, Clone, Copy)]
pub enum Direction {
Up,
Down,
Left,
Right,
}

View File

@ -0,0 +1,25 @@
pub mod part_one;
// pub mod part_two;
pub mod common;
use crate::utils::solution::Solution;
use crate::utils::input_reader;
pub struct DaySolution ;
impl Solution for DaySolution {
fn input_path(&self) -> &'static str {
"input/2023/day_17/input_test.txt"
}
fn part_one(&self) -> String {
input_reader::read_input_file(self.input_path(), part_one::part_one)
}
fn part_two(&self) -> String {
// input_reader::read_input_file(self.input_path(), part_two::part_two)
String::from("Not implemented")
}
}

View File

@ -0,0 +1,111 @@
use std::{collections::{HashMap, HashSet}, cmp};
use super::common::Direction;
pub fn part_one(input_lines: Vec<String>) -> String {
let mut map: Vec<Vec<i32>> = Vec::new();
for line in input_lines {
let mut row: Vec<i32> = Vec::new();
for c in line.chars() {
row.push(c.to_digit(10).unwrap() as i32);
}
map.push(row);
}
let mut cache: HashMap<(i32, i32), i32> = HashMap::new();
let cost = min(
min_cost_path(&map, &mut cache, HashSet::new(), Direction::Right, 0, 0, 0),
min_cost_path(&map, &mut cache, HashSet::new(), Direction::Down, 0, 0, 0),
i32::MAX,
);
cost.to_string()
}
fn min_cost_path(
map: &Vec<Vec<i32>>,
cache: &mut HashMap<(i32, i32), i32>,
mut current_path: HashSet<(i32, i32)>,
dir: Direction,
step_count: i32,
i: i32,
j: i32) -> i32 {
println!("{}: {} {} {:?}", step_count, i, j, dir);
if i < 0 || j < 0 || i >= map.len() as i32 || j >= map[0].len() as i32 {
return i32::MAX;
}
if step_count >= 3 {
return i32::MAX;
}
if current_path.contains(&(i,j)) {
return i32::MAX;
} else {
current_path.insert((i,j));
}
if i == map.len() as i32 - 1 && j == map[0].len() as i32 - 1 {
println!("Final path: {:?}", current_path);
return map[i as usize][j as usize];
}
if cache.contains_key(&(i,j)) {
return *cache.get(&(i,j)).unwrap();
}
let mut cost = 0;
if i != 0 && j != 0 {
cost = map[i as usize][j as usize];
}
let res: i32;
match dir {
Direction::Up => {
res = min(
min_cost_path(map, cache, current_path.clone(), Direction::Up, step_count + 1, i - 1, j),
min_cost_path(map, cache, current_path.clone(), Direction::Left, 0, i, j - 1),
min_cost_path(map, cache, current_path.clone(), Direction::Right, 0, i, j + 1)
);
},
Direction::Down => {
res = min(
min_cost_path(map, cache, current_path.clone(), Direction::Down, step_count + 1, i + 1, j),
min_cost_path(map, cache, current_path.clone(), Direction::Left, 0, i, j - 1),
min_cost_path(map, cache, current_path.clone(), Direction::Right, 0, i, j + 1)
);
},
Direction::Left => {
res = min(
min_cost_path(map, cache, current_path.clone(), Direction::Left, step_count + 1, i, j - 1),
min_cost_path(map, cache, current_path.clone(), Direction::Up, 0, i - 1, j),
min_cost_path(map, cache, current_path.clone(), Direction::Down, 0, i + 1, j)
);
},
Direction::Right => {
res = min(
min_cost_path(map, cache, current_path.clone(), Direction::Right, step_count + 1, i, j + 1),
min_cost_path(map, cache, current_path.clone(), Direction::Up, 0, i - 1, j),
min_cost_path(map, cache, current_path.clone(), Direction::Down, 0, i + 1, j)
);
},
}
current_path.remove(&(i, j));
if let Some(new_cost) = cost.checked_add(res) {
cost = new_cost;
} else {
cost = i32::MAX;
}
cache.insert((i,j), cost);
cost
}
fn min(a: i32, b: i32 ,c: i32) -> i32 {
cmp::min(a, cmp::min(b, c))
}

View File

@ -14,6 +14,7 @@ pub mod day_13_point_of_incidence;
pub mod day_14_parabolic_reflector_dish;
pub mod day_15_lens_library;
pub mod day_16_the_floor_will_be_lava;
pub mod day_17_clumsy_crucible;
pub fn run(day: &str) {
let solution: Box<dyn Solution> = match day {
@ -31,6 +32,7 @@ pub fn run(day: &str) {
"14" => Box::new(day_14_parabolic_reflector_dish::DaySolution),
"15" => Box::new(day_15_lens_library::DaySolution),
"16" => Box::new(day_16_the_floor_will_be_lava::DaySolution),
"17" => Box::new(day_17_clumsy_crucible::DaySolution),
_ => panic!("Invalid day specified"),
};