You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
1.4 KiB
Rust

use regex::Regex;
use std::collections::{HashMap, HashSet};
#[derive(Debug)]
struct Room<'a> {
valve: &'a str,
rate: u8,
tunnels: Vec<&'a str>,
}
fn main() {
let contents = std::fs::read_to_string("test.txt").expect("Failed to read file");
let mut hash: HashMap<&str, Room> = HashMap::new();
contents.lines().for_each(|l| parse(l, &mut hash));
println!("rooms {:?}", hash);
traverse(&mut hash, "AA");
}
fn parse<'a>(str: &'a str, hash: &mut HashMap<&'a str, Room<'a>>) -> () {
let re = Regex::new(r"Valve (.*) has flow rate=(.*); tunnels? leads? to valves? (.*)").unwrap();
let c = re.captures(str).unwrap();
let valve = c.get(1).unwrap().as_str();
let rate = c.get(2).unwrap().as_str().parse().unwrap();
let tunnels = c
.get(3)
.unwrap()
.as_str()
.split(", ")
.collect::<Vec<&str>>();
hash.insert(
valve,
Room {
valve,
rate,
tunnels,
},
);
}
fn traverse(hash: &mut HashMap<&str, Room>, start: &str) -> () {
let mut visited: HashSet<&str> = HashSet::new();
let cur = hash.get(start).unwrap();
// let mut max = 0;
let mut biggest = cur.tunnels.iter().fold((start, 0), |acc, r| {
let room = hash.get(r).unwrap();
if hash.get(r).unwrap().rate > max {
max = room.rate;
r
} else {
acc
}
});
println!("biggest {:?}", biggest);
}