aoc-2023/d08/main.rs

62 lines
1.5 KiB
Rust
Raw Normal View History

2023-12-08 17:36:04 +00:00
use std::collections::HashMap;
use std::fs;
struct Node {
left: String,
right: String,
}
fn main() {
// let file_contents = fs::read_to_string("sample.txt").expect("No input found");
let file_contents = fs::read_to_string("input.txt").expect("No input found");
let mut lines = file_contents.lines();
let steps = lines.next().expect("No steps found");
let map = lines
.filter(|l| !l.is_empty())
.map(|l| {
let node = &l[0..3];
let left = &l[7..10];
let right = &l[12..15];
(
node,
Node {
left: left.to_string(),
right: right.to_string(),
},
)
})
.collect::<HashMap<&str, Node>>();
let mut current = map
.keys()
.filter(|node| node.ends_with("A"))
.map(|node| node.clone())
.collect::<Vec<&str>>();
for node in &current {
println!("Starting node {}", node);
}
let mut num_steps = 0;
for step in steps.chars().cycle() {
if current.iter().all(|node| node.ends_with("Z")) {
break;
}
num_steps += 1;
for i in 0..current.len() {
if step == 'L' {
current[i] = &map[current[i]].left;
}
if step == 'R' {
current[i] = &map[current[i]].right;
}
}
}
println!("Part 2 {}", num_steps)
}