part-02.ts (1088B)
1 import { lineByLine } from './line-by-line'; 2 import { sum } from './sum'; 3 4 let history = []; 5 let sumLast = null; 6 let increasedTotal = 0; 7 8 let texts = [ 9 'decreased', 10 'no change', 11 'increased', 12 ]; 13 14 (async () => { 15 16 await lineByLine(__dirname + '/input', line => { 17 if ((!line) || isNaN(line)) return; 18 const depth = parseInt(line); 19 history.push(depth); 20 21 while (history.length >= 3) { 22 const sumCurrent = sum(...history.slice(0,3)); 23 history.shift(); 24 25 if (null === sumLast) { 26 process.stdout.write(`${sumCurrent} (N/A - no previous measurement)\n`); 27 } else { 28 const increased = (sumCurrent > sumLast) | 0; 29 const decreased = (sumCurrent < sumLast) | 0; 30 increasedTotal += increased; 31 process.stdout.write(`${sumCurrent} (${texts[1 + increased - decreased]})\n`); 32 } 33 34 sumLast = sumCurrent; 35 } 36 37 }); 38 39 process.stdout.write('\n\n'); 40 process.stdout.write('---[ REPORT ]---\n'); 41 process.stdout.write(`Increased ${increasedTotal} times\n`); 42 process.stdout.write('\n'); 43 44 })(); 45