DEV Community

Cover image for Not Quite Lisp
Robert Mion
Robert Mion

Posted on

Not Quite Lisp

Advent of Code 2015 Day 1

Part 1

Ups and downs

  • Where will Santa end up?
  • What better way to find out than with reduce()!

My algorithm in JavaScript:

input.split("").reduce(
  (floor, direction) => floor += direction == "(" ? 1 : -1
, 0)
Enter fullscreen mode Exit fullscreen mode

Part 1, a year ago

My algorithm in JavaScript:

function dayOnePart1(str) {
  return str.split("").reduce((acc, curr, i) => { 
    return curr == "(" ? acc + 1 : acc - 1; 
  }, 0)
}
Enter fullscreen mode Exit fullscreen mode
  • Very similar to how I just did it
  • Although the ternary feels awkward now that I'm familiar with putting assignment in the first clause

Part 2

Finding the first negative

My algorithm in JavaScript:

let floor = 0, i = 0
while (floor !== -1) floor += input[i++] == "(" ? 1 : -1
return i
Enter fullscreen mode Exit fullscreen mode

Part 2, a year ago

My algorithm in JavaScript:

function dayOnePart2(str) {
  let arr = str.split("")
  let firstBasement
  let acc = 0
  for (let i = 0; i < arr.length; i++) {
    if (acc == -1) {
      firstBasement = i
      break;
    }
    arr[i] == "(" ? acc += 1 : acc -= 1;
  }
  return firstBasement  
}
Enter fullscreen mode Exit fullscreen mode
  • A for loop with a break in it? Yikes!
  • This feels so much more complicated!

I did it!!

  • I solved both parts!
  • In three lines of code, each!
  • Using syntax and techniques that I've grown much more familiar with throughout this experience!

Year in review

  • 46 stars earned: tied with my personal best!
  • 2 simulators built: tied with my lowest amount in a year!

The Christmas tree almost all lit up

Star count (2021-2015)

  • 278/350: 79.4 - C+ - Passing grade, still! Yearly star counts

Last words, until December, 2022

I set out with one main goal:

  • Attempt at least Part 1 of all 175 Days

I had other goals:

  • Learn new computer science concepts
  • Practice writing JavaScript
  • Practice Front-End Development by simulating highly-visual puzzles
  • Teach beginners the joys of programming
  • Document everything, especially frustrations, struggles, debugging, failed attempts, confusion and times that I cheated by resorting to any given Day's reddit Solution Megathread
  • Surprise myself by solving tremendously difficult puzzles - at least for my professionally-untrained skill level
  • Create a timeless series that, when discovered, can act as a silent partner along some other budding puzzle-solver's journey

Did I meet every goal? You bet!

Does this end here? Of course not!

This series will continue as long as Eric Wastl continues publishing more puzzles each December.

I encourage you to do like I did, and support Eric:
Payment receipts

See you back here in December, 2022!

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay