For this solution I used a stack. If the unit on top of the stack can react to the incoming unit, I just pop it out of the stack. Otherwise, I push it and move on.
const{readFile}=require('./reader');const{reactPolymer}=require('./05-common');(async()=>{constlines=awaitreadFile('05-input.txt');constpolymer=reactPolymer(lines[0]);console.log(`The remaining units are ${polymer.length}`);})();
05b.js
const{readFile}=require('./reader');const{reactPolymer}=require('./05-common');constdetectUnitTypes=polymer=>{constexistence=newSet();returnpolymer.toLowerCase().split('').filter(unit=>{if(existence.has(unit)){returnfalse;}existence.add(unit);returntrue;});};(async()=>{constlines=awaitreadFile('05-input.txt');constunitTypes=detectUnitTypes(lines[0]);constpolymersWithoutUnit=newMap();for(letunitofunitTypes){constpolymer=reactPolymer(lines[0].replace(newRegExp(unit,'ig'),''));polymersWithoutUnit.set(unit,polymer.length);}constshortestPolymerLength=Math.min(...polymersWithoutUnit.values());console.log(`The shortest polymer length is ${shortestPolymerLength}`);})();
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.
JavaScript solution
For this solution I used a stack. If the unit on top of the stack can react to the incoming unit, I just pop it out of the stack. Otherwise, I push it and move on.
reader.js
05-common.js
05a.js
05b.js