| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- function Poly(str) {
- this.str = str;
- this.units = [];
- for (var i =0, len= str.length; i < len; ++i)
- if (this.units.indexOf(str[i].toLowerCase()) == -1)
- this.units.push(str[i].toLowerCase());
- this.units = this.units.sort();
- }
- Poly.checkReact = function(a, b) {
- return a.toLowerCase() == b.toLowerCase() && a !== b;
- }
- Poly.prototype.doReact = function() {
- for (var i =0, len= this.str.length -1; i < len; ++i)
- if (Poly.checkReact(this.str[i], this.str[i +1])) {
- this.str = this.str.substr(0, i) +this.str.substr(i +2);
- return true;
- }
- return false;
- }
- Poly.prototype.react = function() {
- var i = 0;
- while (this.doReact())
- ++i;
- return i;
- }
- function read(cb) {
- require("fs").readFile('./input', 'utf8', (err, data) => {
- var line = data.split("\n")[0];
- cb(line);
- });
- }
- function ex1() {
- read((polymereStr) => {
- var polymere = new Poly(polymereStr);
- polymere.react();
- console.log(polymere.str.length);
- });
- }
- function ex2() {
- read((polymereStr) => {
- var minPoly = null,
- minValue = 0,
- pos =0,
- poly = new Poly(polymereStr);
- poly.units.forEach((i) => {
- var p = new Poly(polymereStr.replace(new RegExp(i, "ig"), ""));
- p.react();
- if (minPoly ===null || minValue > p.str.length) {
- minPoly = i;
- minValue = p.str.length;
- }
- process.stdout.write('' +(++pos) +"/" +poly.units.length +"\r");
- });
- process.stdout.write("\n");
- console.log(minPoly, minValue);
- });
- }
- //ex1();
- ex2();
|