Ryan is an engineer in the Sacramento Area with a focus in Python, Ruby, and Rust. Bash/Python Exercism mentor. Coding, physics, calculus, music, woodworking. Looking for work!
Got my Rust solution done! After banging my head on it all yesterday, I'm glad I took a break and came back fresh. It seemed to go pretty smoothly the second try at it.
Part 1
/// Day 7: Sum of Its Parts/// /// Unravel the order of instructions with dependenciesusestd::collections::{HashMap,HashSet};usestd::cmp;// Part 1: In what order should the steps be completed?structDependencyGraph{instructions:HashMap<char,Vec<char>>,}implDependencyGraph{pubfnnew()->Self{Self{instructions:HashMap::new()}}pubfnfrom_instructions(text:&str)->Self{letmutdeps=DependencyGraph::new();forlineintext.lines(){letparent=line.chars().take(6).last().unwrap();letchild=line.chars().take(37).last().unwrap();deps.add_dependency(parent,child);}deps}pubfnadd_dependency(&mutself,parent:char,child:char){self.instructions.entry(parent).or_insert(vec![]);letchild_deps=self.instructions.entry(child).or_insert(vec![]);child_deps.push(parent);}pubfnlinearize(&self)->Vec<char>{letmutresults:Vec<char>=vec![];letmutpending:HashSet<char>=self.instructions.keys().cloned().collect();whilepending.len()>0{letmutsatisfied:Vec<char>=self.instructions.iter().filter(|(c,deps)|{pending.contains(c)&&deps.iter().all(|dep|!pending.contains(dep))}).map(|(c,deps)|c.clone()).collect();satisfied.sort();results.push(satisfied[0]);pending.remove(&satisfied[0]);}results}}/// Given lines of dependencies, processes those dependencies into a linear/// ordered string of instructions.pubfnorder_steps(text:&str)->String{letdeps=DependencyGraph::from_instructions(text);deps.linearize().into_iter().collect()}
Part 2
In part 2, I'm pretty sure we just re-invented the event loop? This mega-function could probably be boiled down to helper functions, but I'm OK with this for now.
// Part 2: How long will it take to complete all the steps?implDependencyGraph{/// Calculate how long it would take if each step has a duration and/// you have some elves helping youpubfnassisted_assembly_duration(&self,workers:usize,base_delay:usize)->usize{letmutpending:HashSet<char>=self.instructions.keys().cloned().collect();letmutactive:HashMap<char,usize>=HashMap::new();letmutclock:usize=0;whilepending.len()+active.len()>0{// check for completed stepsletcompleted:Vec<char>=active.iter().filter(|(_c,finish)|clock>=**finish).map(|(c,_finish)|c).cloned().collect();forletterincompleted{active.remove(&letter);}// Get any tasks available to be worked onletmutsatisfied:Vec<char>=self.instructions.iter().filter(|(c,deps)|{pending.contains(c)&&deps.iter().all(|dep|!pending.contains(dep)&&!active.contains_key(dep))}).map(|(c,deps)|c.clone()).collect();satisfied.sort();// Give any free workers a tasklettasks_to_assign=cmp::min(workers-active.len(),satisfied.len());forletterinsatisfied.iter().take(tasks_to_assign){// This job will get done duration + base_delay seconds from nowactive.insert(*letter,DependencyGraph::duration_for(*letter)+base_delay+clock);pending.remove(letter);}clock+=1;}// Un-tick the clock, since the clock ticks at the end.clock-1}/// Calculates how long a letter will take to process/// /// The duration of each letter is increased by one as the letters/// increase, starting at A = 1/// Assumes (correctly) that all letters are capitalfnduration_for(letter:char)->usize{(letterasusize)-('A'asusize)+1}}/// Find out how long to run a set of tasks with helperspubfnassisted_duration(text:&str,workers:usize,base_delay:usize)->usize{letdeps=DependencyGraph::from_instructions(text);deps.assisted_assembly_duration(workers,base_delay)}
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Got my Rust solution done! After banging my head on it all yesterday, I'm glad I took a break and came back fresh. It seemed to go pretty smoothly the second try at it.
Part 1
Part 2
In part 2, I'm pretty sure we just re-invented the event loop? This mega-function could probably be boiled down to helper functions, but I'm OK with this for now.