part2.js 626 B

12345678910111213141516171819202122
  1. const fs = require('fs')
  2. let data = JSON.parse(fs.readFileSync('./Day 2/data.json'))
  3. let invalid = []
  4. data.forEach(element => {
  5. let info = element[0].split(' ')
  6. let info2 = info[0].split('-')
  7. let stringSplit = element[1].split('')
  8. if (stringSplit[info2[0]] === info[1] && stringSplit[info2[1]] === info[1]) {
  9. invalid.push(element)
  10. } else
  11. if (stringSplit[info2[0]] !== info[1] && stringSplit[info2[1]] !== info[1]) {
  12. invalid.push(element)
  13. } else {
  14. console.log(element)
  15. }
  16. });
  17. let answer = data.length - invalid.length
  18. console.log('Valid ' + answer)