DEV Community ๐Ÿ‘ฉโ€๐Ÿ’ป๐Ÿ‘จโ€๐Ÿ’ป

DEV Community ๐Ÿ‘ฉโ€๐Ÿ’ป๐Ÿ‘จโ€๐Ÿ’ป is a community of 966,904 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Cover image for AoC 2015 - Day 8 - Don't use Eval()
Jules
Jules

Posted on • Updated on

AoC 2015 - Day 8 - Don't use Eval()

Long puzzle, short code, today. We have a lot of strings to look at, and need to compare how many characters they take up in a file vs. how many characters they take up in memory. To give a simple example, "Hello World!" is 14 characters in a file (including the quotes), but only 12 in memory.

In the puzzle, we are also asked to look at escaped characters too. Here's Part 1:

Screenshot of Advent of Code puzzle

There are three types of escape sequences we're asked to deal with:

  1. \\ which represents a single backslash
  2. \" which represents a double quote
  3. \xnn where 'nn' is any two hex digits\ and represents a single escaped Ascii character

The easiest way to deal with this is to load each string and apply eval() to it, assuming AoC wouldn't do anything naughty with their puzzle input. But running eval() against unknown strings is generally a bad idea (you could end up running someone else's code inside yours!). Thankfully there's another version of eval() in the Python Abstract Syntax Tree library that evaluates a string without executing it: literal_eval(). This makes the Part 1 code pretty short:

import ast

with open('src/day08.txt') as f:
    lines = [line.rstrip() for line in f]

total_length = 0

for line in lines:
    total_length += len(line)
    total_length -= len(ast.literal_eval(line))

print(total_length)
Enter fullscreen mode Exit fullscreen mode

Part 2 has us going the other way:

Screenshot of Advent of Code puzzle

I'm afraid I stared at this for a long time before I realised I could simply do some simple addition!

Every string is going to be two characters longer, as we'll be adding quotation marks to each end.

As for the quotation marks in the string itself, each will need to be escaped with an extra character, as well as any backslashes. I decided to simply count these characters the same way I counted vowels on Day 5, making for some very short code:

extras = 0

for line in lines:

    #Account for adding quotes back onto the line
    extras +=2
    extras += sum(map(line.count, ['"', '\\']))

print(extras)
Enter fullscreen mode Exit fullscreen mode

[Note that to search for a backslash that you're planning to escape, you need to escape it in the search string! ๐Ÿ˜€]

There are several one liners in the megathread, but I was pretty happy with my code. It's pretty short while still being (imho) pretty readable.

Top comments (0)

๐ŸŒš Browsing with dark mode makes you a better developer.

It's a scientific fact.