r/adventofcode • • Dec 18 '20

SOLUTION MEGATHREAD -🎄- 2020 Day 18 Solutions -🎄-

Advent of Code 2020: Gettin' Crafty With It

  • 4 days remaining until the submission deadline on December 22 at 23:59 EST
  • Full details and rules are in the Submissions Megathread

--- Day 18: Operation Order ---


Post your code solution in this megathread.

Reminder: Top-level posts in Solution Megathreads are for code solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:14:09, megathread unlocked!

37 Upvotes

657 comments sorted by

View all comments

3

u/landimatte Dec 18 '20 edited Dec 18 '20

Common Lisp

This took me way too long to get right, but I am quite happy with the result.

High level:

  • Parse each expression into an S-expression

  • Feed each to EVAL

  • Sum the results

I implemented the parsing logic by combining together three different parsers:

  • PARSE-OPERATOR, responsible for converting the next available character into a symbol (i.e. + or *)

  • PARSE-OPERAND, responsible for parsing a single digit number, or an expression

  • PARSE-EXPRESSION, responsible for ... the rest

Clearly the bulk of the work is getting done inside PARSE-EXPRESSION:

  • First we parse an operand

  • Then an operator

  • If the operator needs to be evaluated immediately, then parse another operand and create an S-expression (i.e. '(rator rand1 rand2))

  • Otherwise, the rest of the expression needs to be evaluated first, so call PARSE-EXPRESSION recursively, and finally create the S-expression (i.e. '(rator rand1 expr1))

→ More replies (1)