Skip to content
Snippets Groups Projects
Commit ea95a0cd authored by Falk Rehse's avatar Falk Rehse
Browse files

Day 12-2

parent 45572c62
No related branches found
No related tags found
No related merge requests found
use petgraph::algo;
use petgraph::graph::Graph;
use petgraph::graph::NodeIndex;
const START_CHAR: u32 = 'S' as u32;
const END_CHAR: u32 = 'E' as u32;
pub fn solve_from_str(input: &str) -> u32 {
let heights = input.split("\n").map(|row| row.chars().collect()).collect();
solve(&heights)
}
pub fn solve(heights: &Vec<Vec<char>>) -> u32 {
let mut graph = Graph::<_, ()>::new();
let mut nodes = Vec::with_capacity(heights.len());
let mut start = Vec::new();
let mut end = None;
for y in 0..heights.len() {
let mut row = Vec::with_capacity(heights[y].len());
for x in 0..heights[y].len() {
let node = graph.add_node(heights[y][x]);
if heights[y][x] == 'a' || heights[y][x] == 'S' {
start.push(node)
}
if heights[y][x] == 'E' {
end = Some(node);
}
row.push(node);
}
nodes.push(row);
}
for y in 0..heights.len() {
for x in 0..heights[y].len() {
add_edges(&mut graph, &nodes, y, x);
}
}
let end = end.expect("Expected an end node!");
let mut distances = Vec::new();
for start in start {
let dijkstra = algo::dijkstra(&graph, start, Some(end), |_| 1);
if let Some(dijkstra) = dijkstra.get(&end) {
distances.push(*dijkstra);
}
}
distances.sort_unstable();
*distances.first().expect("Expected at least one distance!")
}
fn add_edges(graph: &mut Graph<char, ()>, nodes: &Vec<Vec<NodeIndex>>, y: usize, x: usize) {
let current_node = nodes[y][x];
let current_height = get_height(graph[current_node]);
if x > 0 {
if let Some(nodes) = nodes.get(y) {
if let Some(node) = nodes.get(x - 1) {
if get_height(graph[*node]) <= current_height + 1 {
graph.add_edge(current_node, *node, ());
}
}
}
}
if let Some(nodes) = nodes.get(y) {
if let Some(node) = nodes.get(x + 1) {
if get_height(graph[*node]) <= current_height + 1 {
graph.add_edge(current_node, *node, ());
}
}
}
if y > 0 {
if let Some(nodes) = nodes.get(y - 1) {
if let Some(node) = nodes.get(x) {
if get_height(graph[*node]) <= current_height + 1 {
graph.add_edge(current_node, *node, ());
}
}
}
}
if let Some(nodes) = nodes.get(y + 1) {
if let Some(node) = nodes.get(x) {
if get_height(graph[*node]) <= current_height + 1 {
graph.add_edge(current_node, *node, ());
}
}
}
}
fn get_height(char: char) -> u32 {
let charcode = char as u32;
match charcode {
97..=122 => charcode - 96,
START_CHAR => get_height('a'),
END_CHAR => get_height('z'),
_ => panic!("Illegal charcode: {}", charcode),
}
}
......@@ -19,13 +19,14 @@ mod day_10_2;
mod day_11_1;
mod day_11_2;
mod day_12_1;
mod day_12_2;
use std::fs;
fn main() {
let input = fs::read_to_string("input").expect("Could not read input!");
let solution = day_12_1::solve_from_str(input.as_str());
let solution = day_12_2::solve_from_str(input.as_str());
println!("Solution: {}", solution);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment