Finally something not involving grid points. Took wrong path and wasted lot of time
Swift solution here.
structItem:Hashable,CustomStringConvertible{varcount:Intletname:Stringinit(_count:Int,_name:String){self.count=countself.name=name}init(_string:String){letvalue=string.components(separatedBy:" ")self.count=Int(value.first!)??0self.name=value.last!}vardescription:String{get{return"\(count)\(name)"}}funcscaled(byscale:Int)->Item{returnItem(count*scale,name)}}structReaction{varinputs:[Item]varoutput:Item}varreactions=input.map{lineinReaction.init(inputs:line.first.map{$0.map{Item($0)}}!,output:Item(line.last!.first!))}funccost(_count:Int)->Int{varreq:[Item]=[Item.init(count,"FUEL")]varexcess:[String:Int]=[:]vardone=falsewhile!done{//print(req)if!req.contains(where:{$0.name!="ORE"}){done=true}varnext:[Item]=[]foriteminreq{ifitem.name=="ORE"{excess["ORE",default:0]+=item.count}else{//print(excess)varquantity=item.countifletextra=excess[item.name]{ifextra>quantity{excess[item.name,default:0]=extra-quantitycontinue}else{quantity-=extraexcess.removeValue(forKey:item.name)}}letcurrent=reactions.filter{$0.output.name==item.name}.first!letscale=Int(ceil(Double(quantity)/Double(current.output.count)))current.inputs.map{iinvarx=ix.count*=scalenext.append(x)}leta=current.output.count*scale-quantityifa>0{excess[item.name,default:0]+=a}}}req=next}//print(excess)returnreq.filter{(item)->Boolinitem.name=="ORE"}.first?.count??0+(excess["ORE"]??0)}funcpartOne(){letresult=cost(1)print("Part 1 answer is :\(result)")}funcpartTwo(){varlow=0varhigh=Int(1e12)whilelow<high{letmid=(low+high+1)/2ifcost(mid)<=Int(1e12){low=mid}else{high=mid-1}}print("Part 2 answer is :\(low)")}partOne()partTwo()
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.
Finally something not involving grid points. Took wrong path and wasted lot of time
Swift solution here.