Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::collections::HashSet;
- use std::time::Instant;
- use std::fs::read_to_string;
- fn main() {
- let now = Instant::now();
- day12_2fast2();
- println!("{}", now.elapsed().as_millis());
- }
- //FASTEST DAY 12 implementation
- fn day12_2fast2(){
- let input = read_to_string("temp12").expect("AA");
- let paths : Vec<_> = input.lines().map(|x| x.split_once('-').expect("OK")).collect();
- //println!("{:?}", paths);
- let mut walks = walk_path2fast2("start", &paths, "".to_string(), true);
- let mut set = HashSet::new();
- set.extend(walks.iter());
- println!("{}", set.len());
- }
- fn walk_path2fast2(curr_node : &str, paths : &Vec<(&str, &str)>, path_so_far : String, can_double_dip: bool) -> Vec<String> {
- let mut ret = Vec::new();
- if curr_node.eq("end")
- {
- ret.push(path_so_far);
- return ret;
- }
- let is_lower = curr_node.chars().next().expect("").is_lowercase();
- let so_far = path_so_far.to_owned() + "," + curr_node;
- for &path in paths {
- let other_node;
- if path.0.eq(curr_node){
- other_node = path.1
- }
- else if path.1.eq(curr_node) {
- other_node = path.0;
- }
- else {
- continue;
- }
- if (is_lower && !can_double_dip ) || curr_node.eq("start")
- {
- //remove all paths to this node so we cant go back
- let new_paths: Vec<(&str, &str)> = paths.clone().into_iter().filter(|&x| {
- if x.0.eq(curr_node) || x.1.eq(curr_node)
- {
- return false;
- }
- return true;
- }).collect();
- ret.extend(walk_path2fast2(other_node, &new_paths, so_far.to_string(), can_double_dip ));
- }
- else if is_lower && can_double_dip {
- //try a double dip
- ret.extend(walk_path2fast2(other_node, paths, so_far.to_string(), false));
- //remove all paths to this node so we cant go back, and we can still double dip later.
- let new_paths: Vec<(&str, &str)> = paths.clone().into_iter().filter(|&x| {
- if x.0.eq(curr_node) || x.1.eq(curr_node)
- {
- return false;
- }
- return true;
- }).collect();
- ret.extend(walk_path2fast2(other_node, &new_paths, so_far.to_string(), true));
- }
- // big node we dont care.
- else {
- ret.extend(walk_path2fast2(other_node, paths, so_far.to_string(), can_double_dip ))
- }
- }
- return ret;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement