DEV Community

Discussion on: Advent of Code 2020 Solution Megathread - Day 16: Ticket Translation

Collapse
 
kudostoy0u profile image
Kudos Beluga • Edited

Part 1 JS solution, I've been hearing that Part 2 day 16 is a very hard part
😳, hope I'll be able to finish that


const fs = require("fs")
let data = fs.readFileSync("input.txt","utf8"),
rules = data.match(/(.|\n)+(?=\n\syour ticket:)/i)[0].split("\n"),
valids = new Set();
rules.forEach(e => {
  e.match(/\d+-\d+/gi).forEach(e => {
     for (let i = Number(e.match(/\d+/i));i <= Number(e.match(/(?<=\d+-)\d+/)[0]);i++) {
     valids.add(i)
     }
  })
})
let nearbytickets = data.match(/(?<=ts:\n)(.|\n)+/)[0].split("\n");
let invalids = [];
for (let i = 0,n=nearbytickets.length;i < n; i++) {
  let theticket = nearbytickets[i].split(",").map(e=>Number(e));
  for (let j = 0,o=theticket.length; j < o; j++) {
  let isvalid = valids.has(theticket[j]);
  if (!isvalid) invalids.push(theticket[j])

  }
}
if (invalids.length) console.log(invalids.reduce((a,e) => a+e))
else console.log(0)

Enter fullscreen mode Exit fullscreen mode