1
1
mirror of https://github.com/theoludwig/advent_of_code_2023.git synced 2024-12-08 00:45:53 +01:00

feat: day 8 part 2, solution brute force (slow)

This commit is contained in:
Théo LUDWIG 2024-02-13 00:52:59 +01:00
parent ed00f1e3c6
commit 31bcb54699
Signed by: theoludwig
GPG Key ID: ADFE5A563D718F3B
3 changed files with 46 additions and 2 deletions

10
day_8/input_example_3.txt Normal file
View File

@ -0,0 +1,10 @@
LR
11A = (11B, XXX)
11B = (XXX, 11Z)
11Z = (11B, XXX)
22A = (22B, XXX)
22B = (22C, 22C)
22C = (22Z, 22Z)
22Z = (22B, 22B)
XXX = (XXX, XXX)

View File

@ -113,6 +113,35 @@ pub fn part_1(input: &str) -> usize {
steps steps
} }
pub fn part_2(input: &str) -> usize {
let desert_map = DesertMap::from_str(input).unwrap_or_default();
let mut current_step_keys: Vec<String> = desert_map
.nodes
.keys()
.filter(|key| key.ends_with('A'))
.map(|key| key.to_string())
.collect();
let mut steps = 0;
while !current_step_keys
.iter()
.all(|step_key| step_key.ends_with('Z'))
{
let direction_index = steps % desert_map.directions.len();
let current_direction = &desert_map.directions[direction_index];
for current_step_key in current_step_keys.iter_mut() {
let current_step_value =
if let Some(step_value) = desert_map.nodes.get(current_step_key) {
step_value
} else {
break;
};
*current_step_key = current_step_value[current_direction.index()].to_string();
}
steps += 1;
}
steps
}
#[cfg(test)] #[cfg(test)]
mod day_8_tests { mod day_8_tests {
use super::*; use super::*;
@ -131,4 +160,9 @@ mod day_8_tests {
fn test_part_1() { fn test_part_1() {
assert_eq!(part_1(include_str!("../input.txt")), 15871); assert_eq!(part_1(include_str!("../input.txt")), 15871);
} }
#[test]
fn test_part_2_example_3() {
assert_eq!(part_2(include_str!("../input_example_3.txt")), 6);
}
} }

View File

@ -1,8 +1,8 @@
use day_8::part_1; use day_8::{part_1, part_2};
fn main() { fn main() {
let input = include_str!("../input.txt"); let input = include_str!("../input.txt");
println!("- Day 8: Haunted Wasteland -"); println!("- Day 8: Haunted Wasteland -");
println!("Answer Part 1: {}", part_1(input)); println!("Answer Part 1: {}", part_1(input));
// println!("Answer Part 2: {}", part_2(input)); println!("Answer Part 2: {}", part_2(input));
} }