|
|
@@ -0,0 +1,88 @@
|
|
|
+
|
|
|
+const fs = require('fs');
|
|
|
+
|
|
|
+function read(cb) {
|
|
|
+ fs.readFile('./input', 'utf8', (err, data) => {
|
|
|
+ data.split("\n").forEach((line) => {
|
|
|
+ var currentNb = Number.parseInt(line);
|
|
|
+ if (!isNaN(currentNb))
|
|
|
+ return cb.onLine(currentNb);
|
|
|
+ });
|
|
|
+ cb.onDone();
|
|
|
+ });
|
|
|
+}
|
|
|
+
|
|
|
+function Sum(nb) {
|
|
|
+ this.number = 0;
|
|
|
+}
|
|
|
+
|
|
|
+Sum.prototype.onLine = function(number) {
|
|
|
+ this.number += number;
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
+Sum.prototype.onDone = function() {
|
|
|
+ console.log(this.number);
|
|
|
+}
|
|
|
+
|
|
|
+function FqFinder() {
|
|
|
+ Sum.call(this);
|
|
|
+ this.buffer = [0];
|
|
|
+ this.allNumbers = [];
|
|
|
+ this.doneReading = false;
|
|
|
+ this.found = false;
|
|
|
+ this.sqLen = -1;
|
|
|
+ this.sqPos = 0;
|
|
|
+}
|
|
|
+
|
|
|
+FqFinder.prototype.onLine = function(number) {
|
|
|
+ Sum.prototype.onLine.call(this, number);
|
|
|
+ if (this.buffer.indexOf(this.number) >= 0) {
|
|
|
+ this.found = true;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ this.buffer.push(this.number);
|
|
|
+ if (!this.doneReading) {
|
|
|
+ this.allNumbers.push(number);
|
|
|
+ }
|
|
|
+ if (this.sqLen < 0 && this.sqPos > 0 && this.sqPos %2 == 1)
|
|
|
+ this.checkSq();
|
|
|
+ this.sqPos++;
|
|
|
+ if (this.sqLen > 0 && this.sqPos == this.sqLen)
|
|
|
+ this.sqPos = 0;
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
+FqFinder.prototype.checkSq = function() {
|
|
|
+ for (var i =0; i < this.sqPos /2; ++i) {
|
|
|
+ if (this.allNumbers[i] != this.allNumbers[i +this.sqPos /2])
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ this.sqLen = this.sqPos = this.sqPos /2;
|
|
|
+ console.log("Found sequence ! (len=" +this.sqLen +")");
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
+FqFinder.prototype.onDone = function() {
|
|
|
+ this.doneReading = true;
|
|
|
+ if (!this.found) {
|
|
|
+ if (this.sqLen == -1) {
|
|
|
+ this.sqLen = this.sqPos;
|
|
|
+ this.sqPos = 0;
|
|
|
+ }
|
|
|
+ while (this.onLine(this.allNumbers[this.sqPos]));
|
|
|
+ }
|
|
|
+ console.log(this.number);
|
|
|
+}
|
|
|
+
|
|
|
+function ex1() {
|
|
|
+ read(new Sum());
|
|
|
+}
|
|
|
+
|
|
|
+function ex2() {
|
|
|
+ read(new FqFinder());
|
|
|
+}
|
|
|
+
|
|
|
+ex1();
|
|
|
+ex2();
|
|
|
+
|