Skip to content

Latest commit

 

History

History
60 lines (48 loc) · 7.95 KB

08.md

File metadata and controls

60 lines (48 loc) · 7.95 KB

Day 08

Part 1

What is
the number of characters of code for string literals
minus
the number of characters in memory for the values of the strings
in total for the entire file?

const input = String.raw`""
"abc"
"aaa\"aaa"
"\x27"`

const part1 = input
  .split('\n')
  .reduce(
    (sum, line) =>
      sum +
      line.length -
      (line.replace(/\\(\\|"|x[\da-f]{2})/g, 'x').length - 2),
    0
  )
console.log('Part 1:', part1)

Flems link in Part 2.

Part 2

Encode each code representation as a new string.
What is
the total number of characters to represent the newly encoded strings
minus
the number of characters of code in each original string literal?

const part2 = input
  .split('\n')
  .reduce(
    (sum, line) =>
      sum +
      (line.replace(/(\\|"|\\x[\da-f]{2})/g, '\\$1').length + 2) -
      line.length,
    0
  )
console.log('Part 2:', part2)

Try out the final code on flems.io

What did I learn?

Nothing, this was maybe the quickest puzzle so far.

String.raw() was quite crucial in this puzzle, but I was already aware of its existence.