mirror of
https://github.com/theoludwig/advent_of_code_2023.git
synced 2024-12-08 00:45:53 +01:00
perf: improve day 8 - part 1
This commit is contained in:
parent
23bc51e78c
commit
ed00f1e3c6
@ -4,7 +4,7 @@ use std::collections::HashMap;
|
|||||||
#[derive(Debug, Default, PartialEq, Eq, Clone)]
|
#[derive(Debug, Default, PartialEq, Eq, Clone)]
|
||||||
pub struct DesertMap {
|
pub struct DesertMap {
|
||||||
pub directions: Vec<HorizontalDirection>,
|
pub directions: Vec<HorizontalDirection>,
|
||||||
pub nodes: HashMap<String, (String, String)>,
|
pub nodes: HashMap<String, [String; 2]>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FromStr for DesertMap {
|
impl FromStr for DesertMap {
|
||||||
@ -33,9 +33,9 @@ impl FromStr for DesertMap {
|
|||||||
/// let expected_result = DesertMap {
|
/// let expected_result = DesertMap {
|
||||||
/// directions: vec![HorizontalDirection::Right, HorizontalDirection::Left],
|
/// directions: vec![HorizontalDirection::Right, HorizontalDirection::Left],
|
||||||
/// nodes: HashMap::from([
|
/// nodes: HashMap::from([
|
||||||
/// (String::from("AAA"), (String::from("BBB"), String::from("CCC"))),
|
/// (String::from("AAA"), [String::from("BBB"), String::from("CCC")]),
|
||||||
/// (String::from("BBB"), (String::from("DDD"), String::from("EEE"))),
|
/// (String::from("BBB"), [String::from("DDD"), String::from("EEE")]),
|
||||||
/// (String::from("CCC"), (String::from("ZZZ"), String::from("GGG"))),
|
/// (String::from("CCC"), [String::from("ZZZ"), String::from("GGG")]),
|
||||||
/// ])
|
/// ])
|
||||||
/// };
|
/// };
|
||||||
///
|
///
|
||||||
@ -58,10 +58,10 @@ impl FromStr for DesertMap {
|
|||||||
.unwrap_or_default()
|
.unwrap_or_default()
|
||||||
.replace(['(', ')'], "");
|
.replace(['(', ')'], "");
|
||||||
let mut values_line = values_line.split(", ");
|
let mut values_line = values_line.split(", ");
|
||||||
let value = (
|
let value = [
|
||||||
values_line.next().unwrap_or_default().to_string(),
|
values_line.next().unwrap_or_default().to_string(),
|
||||||
values_line.next().unwrap_or_default().to_string(),
|
values_line.next().unwrap_or_default().to_string(),
|
||||||
);
|
];
|
||||||
result.nodes.insert(key.to_string(), value);
|
result.nodes.insert(key.to_string(), value);
|
||||||
}
|
}
|
||||||
Ok(result)
|
Ok(result)
|
||||||
@ -71,8 +71,14 @@ impl FromStr for DesertMap {
|
|||||||
#[derive(Debug, Default, PartialEq, Eq, Copy, Clone)]
|
#[derive(Debug, Default, PartialEq, Eq, Copy, Clone)]
|
||||||
pub enum HorizontalDirection {
|
pub enum HorizontalDirection {
|
||||||
#[default]
|
#[default]
|
||||||
Left,
|
Left = 0,
|
||||||
Right,
|
Right = 1,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl HorizontalDirection {
|
||||||
|
pub const fn index(&self) -> usize {
|
||||||
|
*self as usize
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<char> for HorizontalDirection {
|
impl From<char> for HorizontalDirection {
|
||||||
@ -89,39 +95,21 @@ const KEY_START: &str = "AAA";
|
|||||||
const KEY_END: &str = "ZZZ";
|
const KEY_END: &str = "ZZZ";
|
||||||
|
|
||||||
pub fn part_1(input: &str) -> usize {
|
pub fn part_1(input: &str) -> usize {
|
||||||
let desert_map = DesertMap::from_str(input).unwrap_or_default();
|
let mut desert_map = DesertMap::from_str(input).unwrap_or_default();
|
||||||
let mut steps = 0;
|
let mut steps = 0;
|
||||||
let mut current_step_key = KEY_START.to_string();
|
let mut current_step_key = KEY_START.to_string();
|
||||||
let mut current_direction_index: usize = 0;
|
|
||||||
|
|
||||||
while current_step_key != KEY_END {
|
while current_step_key != KEY_END {
|
||||||
let current_direction = desert_map
|
let direction_index = steps % desert_map.directions.len();
|
||||||
.directions
|
let current_direction = &desert_map.directions[direction_index];
|
||||||
.get(current_direction_index)
|
let current_step_value =
|
||||||
.cloned()
|
if let Some(step_value) = desert_map.nodes.get_mut(¤t_step_key) {
|
||||||
.unwrap_or_else(|| {
|
step_value
|
||||||
current_direction_index = 0;
|
} else {
|
||||||
desert_map
|
break;
|
||||||
.directions
|
};
|
||||||
.get(current_direction_index)
|
current_step_key = current_step_value[current_direction.index()].to_string();
|
||||||
.cloned()
|
|
||||||
.unwrap_or_default()
|
|
||||||
});
|
|
||||||
|
|
||||||
let current_step_value = desert_map
|
|
||||||
.nodes
|
|
||||||
.get(¤t_step_key)
|
|
||||||
.cloned()
|
|
||||||
.unwrap_or_default();
|
|
||||||
current_step_key = match current_direction {
|
|
||||||
HorizontalDirection::Left => current_step_value.0.clone(),
|
|
||||||
HorizontalDirection::Right => current_step_value.1.clone(),
|
|
||||||
};
|
|
||||||
|
|
||||||
current_direction_index += 1;
|
|
||||||
steps += 1;
|
steps += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
steps
|
steps
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user