Advent of Code 2015 - Day 8 - Matchsticks
On the eighth day of Advent of Code 2015 Santa wants to know how much space his newly digitised list will take up when stored.
const part1 = (input: string): number => input .split('\n') .reduce( (total, line) => total + (line.length - eval(line).length), 0 );
Using the dreaded
Although I would refrain from using this approach with untrusted input, in a small puzzle like this it provides a very succinct solution.
Reducing over the input listing tallying up the length differences along the way, we are able to produce the desired answer 🌟.
In part two we are asked to apply an additional encoding on the provided string literals, returning the total difference between the encoded and original input.
const part2 = (input: string): number => input .split('\n') .reduce( (total, line) => total + (JSON.stringify(line).length - line.length), 0 );
Again, we are able to harness another built-in function
JSON.stringify which will provide us with the desired escape sequences and quotes.
Reducing over the input listing we can tally up these length differences and return the answer we require 🌟.