const fs = require("fs") const input = fs.readFileSync("input.txt", "utf-8", err => { err && console.error(err) }).split("\n").filter(str => str.length).map(str => str.split("|")) const isUniqueSegment = digit => { switch(digit.length){ case 2: return 1 case 3: return 7 case 4: return 4 case 7: return 8 } return -1 } const outputs = input.map(thing => thing[1].match(/[a-z]+/g) ) const inputs = input.map(thing => thing[0].match(/[a-z]+/g) ) let uniqueCount = 0, sum = 0 for (let i = 0; i < inputs.length; i++){ let str = "" const outputSet = outputs[i] const inputSet = inputs[i] const oneSet = inputSet.filter(num => num.length === 2)[0] const fourSet = inputSet.filter(num => num.length === 4)[0] outputSet.forEach(input => { if (isUniqueSegment(input) != -1){ str += isUniqueSegment(input) uniqueCount++ return } // chars in common with one or four string const fourCount = Array.from(input).filter(c => Array.from(fourSet).indexOf(c) != -1) const oneCount = Array.from(input).filter(c => Array.from(oneSet).indexOf(c) != -1) if (input.length === 5){ if (fourCount.length === 2){ str += "2" } else { str += oneCount.length === 2 ? "3" : "5" } } if (input.length === 6) { if (fourCount.length === 4){ str += "9" } else { str += oneCount.length === 2 ? "0" : "6" } } }) sum += parseInt(str) } console.log(uniqueCount, sum)