DEV Community

loading...

Discussion on: Advent of Code 2020 Solution Megathread - Day 4: Passport Processing

Collapse
kudostoy0u profile image
Kudos Beluga • Edited

I might have created the dirtiest js code in the world for this

let fs = require("fs"),part2 = true, fields = ['iyr','ecl','byr','hcl','eyr','hgt','pid'],pass = 0;
fs.readFile("input.txt","utf8",(err,data) => {
  if (err) throw err;
data.split(/\n\s/gi).map(e => {
  let isvalid = true;
 for (i in fields) {
   let match = e.match(new RegExp(`(?<=${fields[i]}:)[A-Z#a-z0-9]+`,"g"))
  if (!match) isvalid=false; else if (part2) {
      match = match[0];
     switch (fields[i]) {   
        case "byr":
        if (!(Number(match)>= 1920 && Number(match)<= 2002)) isvalid=false;
        break;
        case "iyr":
        if (!(Number(match)>= 2010 && Number(match)<= 2020)) isvalid=false;
        break;
        case "eyr":
        if (!(Number(match)>= 2020 && Number(match)<= 2030)) isvalid=false;
        break;
        case "hgt":
        if (match.slice(match.length-2,match.length) == "cm") {
          if (!(Number(match.split("").splice(0,match.length-2).join("")) >= 150 && Number(match.split("").splice(0,match.length-2).join("")) <= 193)) isvalid = false;
         } else {
          if (!(Number(match.split("").splice(0,match.length-2).join("")) >= 59 && Number(match.split("").splice(0,match.length-2).join("")) <= 76)) isvalid = false;
        }
        break;
        case "hcl":
          if (!(match.match(/#([a-f0-9]){6}/i))) isvalid = false;
        break;
        case "ecl":
        let haircolor = ["amb","blu","brn","gry","grn","hzl","oth"].map(e => {
          if (match == e) return "valid"
          }).filter(e => e)
        if (!haircolor.length) isvalid = false;
        break;
        case "pid":
        if(!(match.length == 9)) isvalid = false;
        break;
     }}} if (isvalid) pass++}); console.log(pass) })
Enter fullscreen mode Exit fullscreen mode