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)
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)
}
- 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
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
}
- A
for
loop with abreak
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!
Star count (2021-2015)
- 278/350: 79.4 -
C+
- Passing grade, still!
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:
See you back here in December, 2022!
Top comments (0)