I quite liked this puzzle! It took me a few false starts-- I couldn't decide whether to track a step's dependencies or dependents --but ultimately I quite like the way my code turned out. Calling sort on every step seemed...wasteful...to me, but it was fast enough, so it all worked out in the end.
Here's my Julia code (boring parsing, etc. omitted but available here)
function step_ordering(input)step_map=parse_input(input)result=alpha_bfs(step_map)returnresultendfunction alpha_bfs(step_map)roots=find_roots(step_map)result=""seen=Set()while(!isempty(roots))sort!(roots)found=popfirst!(roots)result*=foundforotherinvalues(step_map)delete!(other.prev,found)ifisempty(other.prev)&&!(other.nameinseen)push!(seen,other.name)push!(roots,other.name)endendendreturnresultendfunction find_roots(step_map)all_nodes=Set{String}()fornodeinvalues(step_map)forprevinnode.prevpush!(all_nodes,prev)endendreturncollect(setdiff(all_nodes,keys(step_map)))end
Part 2 was also enjoyable-- I briefly considered actually spawning 5 worker threads and keeping a global clock, but then I realized I could just track how much work was being done in between tracking which nodes are now available.
function costed_alpha_bfs(step_map)roots=find_roots(step_map)result=""seen=Set()total_time=0costs=Dict{String,Int}()base_cost=60max_workers=5workers=max_workerswhile(!isempty(roots)||!isempty(costs))sort!(roots)whileworkers>0&&!isempty(roots)found=popfirst!(roots)costs[found]=cost_of_node(found)+base_costworkers-=1end(cost,found)=findmin(costs)result*=foundtotal_time+=costworkers=min(max_workers,workers+1)delete!(costs,found)forin_progressinkeys(costs)costs[in_progress]-=costendforotherinvalues(step_map)delete!(other.prev,found)ifisempty(other.prev)&&!(other.nameinseen)push!(seen,other.name)push!(roots,other.name)endendendreturntotal_timeend
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.
I quite liked this puzzle! It took me a few false starts-- I couldn't decide whether to track a step's dependencies or dependents --but ultimately I quite like the way my code turned out. Calling
sort
on every step seemed...wasteful...to me, but it was fast enough, so it all worked out in the end.Here's my Julia code (boring parsing, etc. omitted but available here)
Part 2 was also enjoyable-- I briefly considered actually spawning 5 worker threads and keeping a global clock, but then I realized I could just track how much work was being done in between tracking which nodes are now available.