this one was easy (to me!) in comparison to yesterdays, might even be the easiest thus far. All that being said, DAMNNNNNN is part 2 slow, at least with my current impl.
00:07 run time for part 1
02:50 run time for part 2 :OOOOO
publicclassAlchemicalReduction{publicstringReducePolymer(stringpolymer){while(true){StringBuildersb=newStringBuilder();varpreviousChar="";for(inti=0;i<polymer.Length;i++){stringcurrentChar=newstring(new[]{polymer[i]});// Same letterif(currentChar.Equals(previousChar,StringComparison.OrdinalIgnoreCase)){// different caseif(!currentChar.Equals(previousChar,StringComparison.Ordinal)){// Remove the last character from the builder, don't add this charactersb.Remove(i-1,1);// Add the remaining characters to the buildersb.Append(polymer.Substring(i+1,polymer.Length-i-1));// reset the previous char for next entry into for looppreviousChar="";break;}}// Record new previous char, append the current char to the builderpreviousChar=currentChar;sb.Append(currentChar);}// Completed for loo pass, build the stringvarnewString=sb.ToString();// break out of while loop if they're the same string (has been reduced by maximum amount)if(polymer==newString){break;}// Work with the newly modified string within the for looppolymer=newString;}returnpolymer;}publicstringFullyReducePolymerByEliminatingSingleUnit(stringpolymer){varoriginalPolymer=polymer;varnormalizedPolymer=originalPolymer.ToUpper();// get the individual "types" within the polymervargroupsOfTypes=normalizedPolymer.GroupBy(gb=>gb).Select(s=>s.Key);// builds new list of potential polymers, removing a single type from the chain in each runList<string>newPotentialPolymers=newList<string>();foreach(varsingroupsOfTypes){// Removes a single type from the potential polymervarcharToRemove=newstring(new[]{s});varregex=newRegex(charToRemove,RegexOptions.IgnoreCase);newPotentialPolymers.Add(regex.Replace(originalPolymer,""));}// Run the new potential polymersList<string>reducedPolymers=newList<string>();foreach(varpotentialPolymerinnewPotentialPolymers){reducedPolymers.Add(ReducePolymer(potentialPolymer));}// return the smallest onevarminLengthPolymer=reducedPolymers.Min(m=>m.Length);returnreducedPolymers.Where(w=>w.Length==minLengthPolymer).First();}}
Didn't think about the stack approach til seeing other's solutions, wow so much faster! Didn't really occur to me you could accomplish the whole reduction in a single pass through the polymer.
Part 1 went from 00:07 -> 12ms
Part 2 went from 02:50 -> 269ms
publicstringReducePolymer(stringpolymer){// Refactored to stack with input from others solutions at: // https://dev.to/rpalo/aoc-day-5-alchemical-reduction-5b2f/commentsStack<string>stack=newStack<string>();foreach(varcinpolymer){vars=newstring(new[]{c});// the top item in the stack (or empty string if we haven't yet added to stack)vartop=stack.Count==0?string.Empty:stack.Peek();// if the top item is the same letter, // but different case than the currently evaluated character,// remove the top item from the stack, and don't add the current character.if(top!=""&&top.ToLower()==s.ToLower()&&top!=s){stack.Pop();}// No match, add the character to the stackelse{stack.Push(s);}}// Is there a better way to project the stack back into a contiguous string?varsb=newStringBuilder();while(stack.Count>0){sb.Append(stack.Pop());}returnsb.ToString();}
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.
this one was easy (to me!) in comparison to yesterdays, might even be the easiest thus far. All that being said, DAMNNNNNN is part 2 slow, at least with my current impl.
00:07 run time for part 1
02:50 run time for part 2 :OOOOO
Didn't think about the stack approach til seeing other's solutions, wow so much faster! Didn't really occur to me you could accomplish the whole reduction in a single pass through the polymer.
Part 1 went from 00:07 -> 12ms
Part 2 went from 02:50 -> 269ms