Maria is delivering pizzas to an infinite two-dimensional grid of houses. She begins by delivering a pizza to the house at her starting location, then a dispatcher calls via radio and tells her where to move next. Moves are always exactly one house to the north (^), south (v), east (>), or west (<). After each move, Maria delivers a pizza to the house at her new location. The dispatcher has been a little spacey lately, so Maria sometimes ends up delivering more than one pizza to the same house.
Here are some examples:
-
> delivers pizzas to two houses: one to the house at the starting location, and one to the house directly east of the starting location.
-
^>v< delivers pizzas to four houses in a square; the house at the starting/ending location ends up with two pizzas.
-
^v^v^v^v^v delivers a bunch of pizzas to some very lucky people at only two houses.
The question: Given the string of dispatcher inputs attached to this gist, how many houses receive at least one pizza?
The next day, to speed up the process, Maria rents a pizza-delivering goat named Clovis. Maria and Clovis begin at the same starting location, and they both deliver a pizza to this starting house. Maria and Clovis then take turns moving based on the dispatcher's instructions.
Here are some examples:
-
^v now delivers pizzas to three houses; Maria goes north and Clovis goes south.
-
^>v< now delivers pizzas to three houses; Maria and Clovis both end up back where they started.
-
^v^v^v^v^v now delivers pizzas to 11 houses; Maria treks north and Clovis treks south.
The question: Given the same string of dispatcher inputs as in Part One, how many houses receive at least one pizza?
Please provide a solution in JavaScript (any flavor is acceptable) or CoffeeScript.
By design, this is a fairly simple exercise. The goal isn't to task you, job-seeker, with a days-long puzzle, especially since you are likely applying to other jobs, which probably involve jumping through other hoops. What we want to do is get a quick baseline on your coding proficiency, and use the code you write as the basis for discussion in a technical interview.
We're looking for a solution that is elegant and modular as well as correct. We'd like to see maximal code reuse between the solutions to Part One and Part Two. Extra points for a functional style. Feel free to pull in any libraries you like, and try to write code that you'd imagine committing to a real production codebase.