part2.js 781 B

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