advent-of-code

Entries to advent of code, multiple years
git clone git://git.finwo.net/misc/advent-of-code
Log | Files | Refs

part-01.ts (1063B)


      1 import { lineByLine } from './line-by-line';
      2 
      3 let values = null;
      4 let lines  = 0;
      5 
      6 (async () => {
      7 
      8   await lineByLine(__dirname + '/input', line => {
      9     if (!line) return;
     10 
     11     const lineValues = line.split('');
     12     lines++;
     13 
     14     if (!values) {
     15       values = new Array(lineValues.length).fill(0);
     16     }
     17 
     18     lineValues.forEach((bit, index) => {
     19       values[index] += parseInt(bit, 2);
     20     });
     21   });
     22 
     23   // Tasks questions
     24   const commons = values.map((a,i) => (a >= (lines / 2) ? 1 : 0));
     25   const gamma   = parseInt(commons.join(''), 2);
     26   const epsilon = parseInt(commons.map(a => 1-a).join(''), 2);
     27   const power   = gamma * epsilon;
     28 
     29   process.stdout.write('\n\n');
     30   process.stdout.write('---[ REPORT ]---\n');
     31   process.stdout.write('\n');
     32   process.stdout.write(`Values (total: ${lines})\n`);
     33   process.stdout.write(`  ${values.join(',')}\n`);
     34   process.stdout.write('\n');
     35   process.stdout.write(`Gamma  : ${gamma}\n`);
     36   process.stdout.write(`Epsilon: ${epsilon}\n`);
     37   process.stdout.write(`Power  : ${power}\n`);
     38   process.stdout.write('\n');
     39 
     40 
     41 })();
     42