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) })
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I might have created the dirtiest js code in the world for this