Created
January 11, 2024 20:40
-
-
Save merriam/d5f63bc6cb279812396006ca1010fe6a to your computer and use it in GitHub Desktop.
Rust creating two outputs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate regex; | |
use regex::Regex; | |
use std::collections::HashMap; | |
type NodeList = HashMap<String, (String, String)>; | |
fn parse_input(input_filename: &str) -> (String, NodeList) { | |
let contents = std::fs::read_to_string(input_filename).unwrap(); | |
let mut pieces = contents.lines(); // mut for an iterator | |
let left_rights = pieces.next().unwrap().trim(); | |
println!("left_rights=:{left_rights}:"); | |
pieces.next(); | |
// let nodes: Vec<&str> = pieces.collect(); | |
// dbg!("The LR codes are {left_rights"); | |
// dbg!(nodes); | |
let mut nodes: HashMap<String, (String, String)> = HashMap::new(); | |
let regex = Regex::new(r"(?m)([A-Z0-9]{3}) = \(([A-Z0-9]{3}), ([A-Z0-9]{3})\)").unwrap(); | |
for line in pieces { | |
let groups = regex.captures(line).unwrap(); | |
let from_node = groups.get(1).unwrap().as_str().to_string(); | |
let left = groups.get(2).unwrap().as_str().to_string(); | |
let right = groups.get(3).unwrap().as_str().to_string(); | |
// println!("New node: ({from_node} -> {left}, {right})"); | |
// let pair = (left, right); | |
// nodes.insert(from_node.clone(), pair); | |
nodes.insert(from_node, (left, right)); | |
} | |
println!("Nodes =\n{:?}", nodes); | |
(left_rights.to_string(), nodes) | |
} | |
fn main() { | |
println!("Hello, world!"); | |
let (left_rights, nodes) = parse_input("day8samp.txt"); | |
let start_string_iter = nodes.keys().filter(|key| key.ends_with('A')); | |
let mut positions = start_string_iter | |
.map(|s| s.as_str()) | |
.collect::<Vec<&str>>(); | |
// let mut positions : Vec<&String>= start_iter.collect(); // starting positions | |
println!("{:?}", positions); | |
let mut steps = 0; | |
let mut lr_iter = left_rights.chars().cycle(); | |
loop { | |
let all_end_with_z = positions.iter().all(|s| s.ends_with('Z')); | |
if all_end_with_z || steps > 100000 { | |
break | |
} | |
let lr = lr_iter.next().unwrap(); | |
if positions.get(0).unwrap().ends_with('Z') { | |
println!("After {steps} steps, next char is {lr}, positions are {:?}", positions); | |
} | |
steps += 1; | |
for pos in &mut positions { | |
let p = next_position(pos, lr, &nodes); | |
*pos = p; | |
} | |
} | |
println!("Final positions are {:?}", positions); | |
println!("Ran for after {steps} steps."); | |
println!("This program is gratified to be of use."); | |
} | |
fn next_position<'a>(pos: &'a str, l_or_r: char, nodes: &'a NodeList) -> &'a str { | |
let lr_tuple = nodes.get(pos).unwrap(); | |
return if l_or_r == 'L' { | |
lr_tuple.0.as_str() | |
} else { | |
lr_tuple.1.as_str() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment