Advent of Code 2017 in #rstats: Day 9

I write less deeply-nested code now that I program in R.  When writing code poorly  in other programs, I’d often use nested IF statements (hi, Excel!).  Debugging that often looked like counting my nesting depth out loud: add one for every (,  subtract one for every ).

That strategy formed the basis for my solution today.   And I was excited to do Part 2 with arithmetic, not writing new code.

Part 1

Strategy: maintain counter of open braces, decreasing for closed braces.

So {{<a>},{<a>},{<a>},{<a>}} = 1 2 2 2 2. Sum = 9.

Part 2

The answer is the total number of characters, minus:

  • The characters canceled by !
  • The bracketing <> for each garbage string
  • The valid characters remaining after removing the garbage in Part 1

To wit:

 

Leave a Reply

Your email address will not be published. Required fields are marked *