horvathm

Untitled

Dec 16th, 2020
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const [rds, yds, nds] = require('fs').readFileSync('input', 'utf8').trim().split('\n\n');
  2.  
  3. const rules = rds.split('\n').map(rd => {
  4.     const m = rd.match(/([a-z ]+): (\d+)-(\d+) or (\d+)-(\d+)/);
  5.     return {
  6.         n: m[1],
  7.         r1: [parseInt(m[2], 10), parseInt(m[3], 10)],
  8.         r2: [parseInt(m[4], 10), parseInt(m[5], 10)]
  9.     };
  10. });
  11.  
  12. const valid = nds.split('\n').slice(1)
  13.     .map(r => r.split(',').map(s => parseInt(s, 10)))
  14.     .filter(t => t.every(n => rules.some(r =>
  15.         (n >= r.r1[0] && n <= r.r1[1]) ||
  16.         (n >= r.r2[0] && n <= r.r2[1])
  17.     )));
  18.  
  19. const fields = rules.map(r => r.n).reduce((p, c) => ({ ...p, [c]: true }), {});
  20. const canbe = Array.apply(null, Array(valid[0].length)).map(v => ({ ...fields }));
  21.  
  22. for (const t of valid) {
  23.     for (const i in t) {
  24.         const n = t[i];
  25.         for (const r of rules) {
  26.             if ((n < r.r1[0] || n > r.r1[1]) && (n < r.r2[0] || n > r.r2[1])) {
  27.                 delete canbe[i][r.n];
  28.             }
  29.         }
  30.     }
  31. }
  32.  
  33. const is = [];
  34.  
  35. for (let f = 0; f < canbe.length; f++) {
  36.     const next = Object.keys(canbe.filter(c => Object.keys(c).length === 1)[0])[0];
  37.  
  38.     for (const i in canbe) {
  39.         const c = canbe[i];
  40.         if (Object.keys(c).length === 1) {
  41.             is[i] = next;
  42.         }
  43.         delete c[next];
  44.     }
  45. }
  46.  
  47. const your = yds.split('\n')[1]
  48.     .split(',').map(s => parseInt(s, 10));
  49.  
  50. let mul = 1;
  51.  
  52. for (const i in is) {
  53.     if (/^departure/.test(is[i])) {
  54.         console.log(i, your[i]);
  55.         mul *= your[i];
  56.     }
  57. }
  58.  
  59. console.log(mul);
  60.  
Advertisement
Add Comment
Please, Sign In to add comment