Advent of Code - Year 2015, Day 03
Problem statement: http://adventofcode.com/2015/day/3
Solving this problem requires keeping track of the houses that Santa has visited. Two ways this can be done: keep a
Set<> of the places where Santa has been, or convert the characters into an enumerable of positions and count the distinct set. I chose to do the latter in both C# and F#.
The tricky part of Part B is keeping track of two current positions, one for Santa and one for robo-Santa. Once you can have a list of all positions for both Santas, then counting the distinct set is the same.
For both parts, I used a poor man's
.Scan() by keeping track of the current position as a side effect variable. This means the code is not functional code, but it's still relatively clean and it gets the job done. This is another case where the new C# 7.0 tuples really help out.
Unfortunately, for part B, I had to duplicate the core code for moving a Santa, because of the interactions with side-effects. It would have been nice to abstract that into a separate function, and I probably could have done that if I spent more time on it.
The F# is easier on this one because the bulk of the work is done in
Seq.scan which C# does not have through LINQ. Since the function for moving a Santa from house to house based on a character is the same, I was able to abstract that and reference it in both parts.
In part B, I kept track of both positions, alternating for each instruction, and then mapped the second position in each tuple into a single set.