Attempt day8 part2

Currently runs forever, never exits
This commit is contained in:
Blizzard Finnegan 2023-12-09 13:11:04 -05:00
parent 1d53183b7a
commit 517f1af936
Signed by: blizzardfinnegan
GPG key ID: 61C1E13067E0018E
3 changed files with 153 additions and 0 deletions

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

@ -0,0 +1,35 @@
--- Part Two ---
The sandstorm is upon you and you aren't any closer to escaping the wasteland. You had the camel follow the instructions, but you've barely left your starting position. It's going to take significantly more steps to escape!
What if the map isn't for people - what if the map is for ghosts? Are ghosts even bound by the laws of spacetime? Only one way to find out.
After examining the maps a bit longer, your attention is drawn to a curious fact: the number of nodes with names ending in A is equal to the number ending in Z! If you were a ghost, you'd probably just start at every node that ends with A and follow all of the paths at the same time until they all simultaneously end up at nodes that end with Z.
For example:
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)
Here, there are two starting nodes, 11A and 22A (because they both end with A). As you follow each left/right instruction, use that instruction to simultaneously navigate away from both nodes you're currently on. Repeat this process until all of the nodes you're currently on end with Z. (If only some of the nodes you're on end with Z, they act like any other node and you continue as normal.) In this example, you would proceed as follows:
Step 0: You are at 11A and 22A.
Step 1: You choose all of the left paths, leading you to 11B and 22B.
Step 2: You choose all of the right paths, leading you to 11Z and 22C.
Step 3: You choose all of the left paths, leading you to 11B and 22Z.
Step 4: You choose all of the right paths, leading you to 11Z and 22B.
Step 5: You choose all of the left paths, leading you to 11B and 22C.
Step 6: You choose all of the right paths, leading you to 11Z and 22Z.
So, in this example, you end up entirely on nodes that end in Z after 6 steps.
Simultaneously start on every node that ends with A. How many steps does it take before you're only on nodes that end with Z?

108
day08/src/bin/second.rs Normal file
View file

@ -0,0 +1,108 @@
use std::fs;
const ELEMENT_SEPARATOR:char = '=';
const SIDE_SEPARATOR:char = ',';
const LEFT:char = 'L';
const RIGHT:char = 'R';
const BEGIN:char = 'A';
const END:char = 'Z';
const FILE_NAME:&str = "./data/data.txt";
fn main() {
let file_contents = fs::read_to_string(FILE_NAME).unwrap();
let mut lines:Vec<&str> = file_contents.split('\n').collect();
let directions_string = lines.remove(0);
let directions:Vec<char> = directions_string.chars().collect();
let mut elements:Vec<Element> = Vec::new();
let mut begins: Vec<Element> = Vec::new();
_ = lines.remove(0);
for line in lines{
if line.is_empty() { break; }
let (element,sides) = line.split_once(ELEMENT_SEPARATOR).unwrap();
let element = element.trim();
let sides = sides.trim();
let (left,right) = sides.split_once(SIDE_SEPARATOR).unwrap();
let left:String = left.trim().chars().filter(|c| c.is_alphanumeric()).collect();
let right:String = right.trim().chars().filter(|c| c.is_alphanumeric()).collect();
let mut front_element = Element::new(element.to_string());
front_element.left_side = left;
front_element.right_side = right;
if element.contains(BEGIN) { begins.push(front_element.clone()); }
elements.push(front_element);
}
let mut index = 0;
loop {
let mut breakpoint = true;
let side = *directions.get(index % directions.len()).unwrap();
let next_locations = get_next_location(side, begins.clone(), elements.clone());
let mut next_begins:Vec<Element> = Vec::new();
index += 1;
for next in next_locations.iter(){
print!("\t{:?}",next);
if !next.chars().last().unwrap().eq_ignore_ascii_case(&END){
breakpoint = false; break;
}
}
println!("\t\t{:?}",index);
if breakpoint { break; }
else{
for next in next_locations{
next_begins.push(Element::new(next));
}
begins = next_begins;
}
}
println!("{:?}",index);
}
fn get_next_location(side:char,current_locations:Vec<Element>,elements:Vec<Element>) -> Vec<String>{
let mut return_vec:Vec<String> = Vec::new();
for current_location in current_locations{
let current_location_name = current_location.get_name().to_string();
let mut next_location_name = String::new();
for element in elements.iter(){
if element.get_name() == current_location_name.clone(){
match side{
LEFT => {
next_location_name = element.left();
break;
},
RIGHT => {
next_location_name = element.right();
break;
},
_ => { panic!();}
}
}
};
print!("{:?}",next_location_name);
return_vec.push(next_location_name);
};
return return_vec;
}
#[derive(Clone,Debug)]
struct Element{
name:String,
left_side:String,
right_side:String,
}
impl Element{
fn new(name:String) -> Self { Self{name, left_side:String::new(),right_side:String::new()} }
fn get_name(&self) -> &str { &self.name }
fn left(&self) -> String { self.left_side.clone() }
fn right(&self) -> String { self.right_side.clone() }
}
impl PartialEq for Element{
fn eq(&self, other: &Self) -> bool { self.name.eq(&other.name) }
}