Advent of Code 2015 in F# - Day 1
Day 1: Not Quite Lisp.
We get a long list of parentheses as input. A
( means “go up one floor”, a
means “go down one floor”.
The goal is to find the final floor after following all instructions. To achieve
this, we parse the parentheses as either
-1, and then simply sum them
let parse c = match c with | '(' -> 1 | ')' -> -1 | _ -> failwithf "Unexpected character %c" c let input = System.IO.File.ReadAllText(filename).Trim() |> Seq.map parse input |> Seq.sum |> printfn "Part 1: %i"
We need to find the index of the first instruction that causes Santa to enter the basement floor. Let’s start with the full solution, and then break it down:
input |> Seq.scan (+) 0 |> Seq.findIndex ((=) -1) |> printfn "Part 2: %i"
The trick here is
|> Seq.scan (+) 0
Seq.scan works like
Seq.fold, but instead of returning the final result as a
single value, each step of the fold is saved and a list of these steps is
returned instead. An example:
Seq.scan (+) 0 [1; 0; 2] // Returns [1; 1; 3]
Once we have this,
Seq.findIndex gives us our solution.