Advent of Code - Year 2015, Day 01
Problem statement: http://adventofcode.com/2015/day/1
Part A simply requires counting the number of
) and evaluating the difference. This can even be done with a basic text editor.
Having to identify the character at which the elevator first moves to the basement requires connecting the current elevator position at each character with the character index. A simple
for loop is the best way to track both data points simultaneously.
If I was just doing part A, I would probably use a
.GroupBy() to split the characters and subtract the counts. Re-doing this in C# 7 allows the use of tuples, which makes the for loop a bit cleaner
This is my first real interaction with F#. I'm probably not using it correctly, since I'm using mutable variables and iterative code. I know F# is geared more towards pure functions, but I don't yet know enough about how to use F# to be able to write this better for F# standards.
Apparently F# provides
Seq.scan, which provides running total values, which is exactly what I really wanted. The resulting list starts with
0 and contains the elevator level at each character. Reversing the list and taking the first value gives the final level, and finding the index of first
-1 value gives the position at which the elevator first goes to the basement. Since the list is prefixed with the initial
0 level, the index is 1-based instead of 0-based, which gives us the answer we need.