Metadata
-
Date
-
Tagged
-
Part of series
-
Older post
-
Newer post
Advent of Code 2015 Day 1
Day 1: Not Quite Lisp
https://adventofcode.com/2015/day/1
Santa’s weather machine’s snow function is powered by stars. During Advent of Code, each puzzle solution rewards you with a star. Hooray, a convenient way to help.
Santa’s in an apartment building and has directions that tell him to go up or down 1 floor.
(
go up one floor)
go down one floor
An example input looks like this:
The building is very tall (and deep!), Santa will never reach the top (or bottom) floor.
Santa starts at the ground floor (number 0
).
Part 1
The question asks what floor Santa ends at after following the instructions.
This asks to turn a sequence of something into a singular answer.
Ideal for your favourite language’s method that does that.
Rust has fold
, JavaScript has reduce
.
Main code for part 1
Part 2
The question asks for the position of the first character that causes Santa to enter the basement.
The basement starts at -1
.
This was fun, I made 3 variants that do the same thing.
Old reliable
A workhorse.
A for
loop.
A short-circuiting fold.
In other words, one that stops as soon as a condition is met.
My favourite
And finally, my favourite, a solution that creates a sequence of the floor Santa is currently on.