TheTintin

Untitled

Nov 26th, 2020
520
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const magic = (entries, start, end) => {
  2.     let value = entries[start]
  3.  
  4.     for (let i = start + 1; i <= end; i++) {
  5.         value = value ^ entries[i]
  6.     }
  7.  
  8.     return value
  9. }
  10.  
  11. const contestResponse = input => {
  12.  
  13.     const entries = input[1].split(' ').map(Number)
  14.  
  15.     const instructions = input
  16.         .slice(2)
  17.         .map(x => {
  18.             const s = x.split(' ')
  19.             return { start: Number(s[0]), end: Number(s[1]) }
  20.         })
  21.  
  22.     const result = instructions.map(i => magic(entries, i.start, i.end))
  23.  
  24.     const counts = {}
  25.  
  26.     result.forEach(element => {
  27.         if (!counts[element]) counts[element] = 0
  28.         counts[element]++
  29.     })
  30.  
  31.     let binary = '' + (counts[0] || 0).toString()
  32.  
  33.     for (let i = 1; i < 256; i++) {
  34.         binary += ' ' + (counts[i] || 0).toString()
  35.     }
  36.  
  37.     return binary
  38. }
  39.  
  40. module.exports = contestResponse
RAW Paste Data