Attempted part 2 but have made a mistake in how I simulatneously traverse them
This commit is contained in:
parent
f76c9794bb
commit
4a095e2a74
@ -1,9 +1,10 @@
|
|||||||
RL
|
LR
|
||||||
|
|
||||||
AAA = (BBB, CCC)
|
11A = (11B, XXX)
|
||||||
BBB = (DDD, EEE)
|
11B = (XXX, 11Z)
|
||||||
CCC = (ZZZ, GGG)
|
11Z = (11B, XXX)
|
||||||
DDD = (DDD, DDD)
|
22A = (22B, XXX)
|
||||||
EEE = (EEE, EEE)
|
22B = (22C, 22C)
|
||||||
GGG = (GGG, GGG)
|
22C = (22Z, 22Z)
|
||||||
ZZZ = (ZZZ, ZZZ)
|
22Z = (22B, 22B)
|
||||||
|
XXX = (XXX, XXX)
|
@ -14,37 +14,38 @@ impl Solution for Day08 {
|
|||||||
|
|
||||||
self.populate_map(&input[2..input.len()], &mut map)?;
|
self.populate_map(&input[2..input.len()], &mut map)?;
|
||||||
|
|
||||||
let mut count = 0u32;
|
Ok(Box::new(self.traverse_to_end(
|
||||||
let mut current = map.get("AAA").unwrap();
|
&"AAA".to_owned(),
|
||||||
let mut found = false;
|
&"ZZZ".to_owned(),
|
||||||
|
&instructions,
|
||||||
while !found {
|
&map,
|
||||||
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))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn part2(
|
fn part2(
|
||||||
&self,
|
&self,
|
||||||
_input: &mut Vec<String>,
|
input: &mut Vec<String>,
|
||||||
) -> Result<Box<dyn std::fmt::Display + Sync>, Box<dyn std::error::Error>> {
|
) -> 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 {
|
fn get_day(&self) -> u8 {
|
||||||
@ -53,6 +54,8 @@ impl Solution for Day08 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl 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>(
|
fn populate_map<'a>(
|
||||||
&self,
|
&self,
|
||||||
input: &[String],
|
input: &[String],
|
||||||
@ -78,6 +81,39 @@ impl Day08 {
|
|||||||
|
|
||||||
Ok(())
|
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
|
/// Test from puzzle input
|
||||||
@ -117,6 +153,6 @@ mod test {
|
|||||||
.unwrap()
|
.unwrap()
|
||||||
.to_string();
|
.to_string();
|
||||||
|
|
||||||
assert_eq!(answer, "Ready");
|
assert_eq!(answer, "6");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user