Attempted part 2 but have made a mistake in how I simulatneously traverse them
This commit is contained in:
		@@ -1,9 +1,10 @@
 | 
			
		||||
RL
 | 
			
		||||
LR
 | 
			
		||||
 | 
			
		||||
AAA = (BBB, CCC)
 | 
			
		||||
BBB = (DDD, EEE)
 | 
			
		||||
CCC = (ZZZ, GGG)
 | 
			
		||||
DDD = (DDD, DDD)
 | 
			
		||||
EEE = (EEE, EEE)
 | 
			
		||||
GGG = (GGG, GGG)
 | 
			
		||||
ZZZ = (ZZZ, ZZZ)
 | 
			
		||||
11A = (11B, XXX)
 | 
			
		||||
11B = (XXX, 11Z)
 | 
			
		||||
11Z = (11B, XXX)
 | 
			
		||||
22A = (22B, XXX)
 | 
			
		||||
22B = (22C, 22C)
 | 
			
		||||
22C = (22Z, 22Z)
 | 
			
		||||
22Z = (22B, 22B)
 | 
			
		||||
XXX = (XXX, XXX)
 | 
			
		||||
@@ -14,37 +14,38 @@ impl Solution for Day08 {
 | 
			
		||||
 | 
			
		||||
        self.populate_map(&input[2..input.len()], &mut map)?;
 | 
			
		||||
 | 
			
		||||
        let mut count = 0u32;
 | 
			
		||||
        let mut current = map.get("AAA").unwrap();
 | 
			
		||||
        let mut found = false;
 | 
			
		||||
 | 
			
		||||
        while !found {
 | 
			
		||||
            for i in instructions.chars() {
 | 
			
		||||
                count += 1;
 | 
			
		||||
                let next: &String;
 | 
			
		||||
                match i {
 | 
			
		||||
                    'L' => next = ¤t.0,
 | 
			
		||||
                    'R' => next = ¤t.1,
 | 
			
		||||
                    _ => {
 | 
			
		||||
                        continue;
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
                if next == "ZZZ" {
 | 
			
		||||
                    found = true;
 | 
			
		||||
                    break;
 | 
			
		||||
                }
 | 
			
		||||
                current = map.get(next).unwrap();
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        Ok(Box::new(count))
 | 
			
		||||
        Ok(Box::new(self.traverse_to_end(
 | 
			
		||||
            &"AAA".to_owned(),
 | 
			
		||||
            &"ZZZ".to_owned(),
 | 
			
		||||
            &instructions,
 | 
			
		||||
            &map,
 | 
			
		||||
        )?))
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn part2(
 | 
			
		||||
        &self,
 | 
			
		||||
        _input: &mut Vec<String>,
 | 
			
		||||
        input: &mut Vec<String>,
 | 
			
		||||
    ) -> Result<Box<dyn std::fmt::Display + Sync>, Box<dyn std::error::Error>> {
 | 
			
		||||
        Ok(Box::new("Ready"))
 | 
			
		||||
        let instructions: String = input[0].clone();
 | 
			
		||||
 | 
			
		||||
        let mut map: HashMap<String, (String, String)> = HashMap::new();
 | 
			
		||||
        self.populate_map(&input[2..input.len()], &mut map)?;
 | 
			
		||||
 | 
			
		||||
        let mut start_points = vec![];
 | 
			
		||||
        for route in &input[2..input.len()] {
 | 
			
		||||
            if route.as_bytes()[2] as char == 'A' {
 | 
			
		||||
                start_points.push(route.split_at(3).0.to_owned());
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        let mut count = 0u32;
 | 
			
		||||
 | 
			
		||||
        println!("{:?}", start_points);
 | 
			
		||||
        for start in start_points {
 | 
			
		||||
            count += self.traverse_to_end(&start, &"Z".to_owned(), &instructions, &map)?
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        Ok(Box::new(count))
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn get_day(&self) -> u8 {
 | 
			
		||||
@@ -53,6 +54,8 @@ impl Solution for Day08 {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Day08 {
 | 
			
		||||
    /// Given a string in the format XXX = (XXX, XXX), will populate a map witht he Xs
 | 
			
		||||
    /// To create a way of traversing the route
 | 
			
		||||
    fn populate_map<'a>(
 | 
			
		||||
        &self,
 | 
			
		||||
        input: &[String],
 | 
			
		||||
@@ -78,6 +81,39 @@ impl Day08 {
 | 
			
		||||
 | 
			
		||||
        Ok(())
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// Function to traverse the map going left and right based on given instructions
 | 
			
		||||
    fn traverse_to_end(
 | 
			
		||||
        &self,
 | 
			
		||||
        start: &String,
 | 
			
		||||
        end: &String,
 | 
			
		||||
        instructions: &String,
 | 
			
		||||
        map: &HashMap<String, (String, String)>,
 | 
			
		||||
    ) -> Result<u32, Box<dyn std::error::Error>> {
 | 
			
		||||
        let mut count = 0u32;
 | 
			
		||||
        let mut current = map.get(start).unwrap();
 | 
			
		||||
        let mut found = false;
 | 
			
		||||
 | 
			
		||||
        while !found {
 | 
			
		||||
            for i in instructions.chars() {
 | 
			
		||||
                count += 1;
 | 
			
		||||
                let next: &String;
 | 
			
		||||
                match i {
 | 
			
		||||
                    'L' => next = ¤t.0,
 | 
			
		||||
                    'R' => next = ¤t.1,
 | 
			
		||||
                    _ => {
 | 
			
		||||
                        continue;
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
                if next.ends_with("ZZZ") {
 | 
			
		||||
                    found = true;
 | 
			
		||||
                    break;
 | 
			
		||||
                }
 | 
			
		||||
                current = map.get(next).unwrap();
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        Ok(count)
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Test from puzzle input
 | 
			
		||||
@@ -117,6 +153,6 @@ mod test {
 | 
			
		||||
            .unwrap()
 | 
			
		||||
            .to_string();
 | 
			
		||||
 | 
			
		||||
        assert_eq!(answer, "Ready");
 | 
			
		||||
        assert_eq!(answer, "6");
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user