summaryrefslogtreecommitdiff
path: root/perfectly-spherical-houses-in-a-vacuum/README.org
blob: 39fd3d6be963b6368bb4b510325db427ac030cce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
See: https://adventofcode.com/2015/day/3
** Part 1
*** Purpose
Given a list of directional arrows:

#+begin_example
^>><v
#+end_example

Determine how many unique spaces on a grid are reached.

*** Method

As a more creative solution, we can use closure to create an environment which keeps track of visited spaces. Say this environment contains an initial variable with this value:

#+begin_example
'((0 . 0))
#+end_example

And we translate each instruction to a different pair of values, each representing a direction.

^>><v:

#+begin_example
'((0 . 1)
  (1 . 0)
  (1 . 0)
  (-1 . 0)
  (0 . -1))
#+end_example

Then simply call the procedure that is encapsulated by a local environment, and have it add the cons pair to the previous and update its internal state.

Finally, we can have another procedure that returns the length of the local list.

** Part 2
*** Purpose
The next part claims we now have two visitors. Each of them take turns receiving directions. I chose an object-like approach for part one because the original problem claimed we were tracking "Santa" visiting houses---this is a good thing, which truly shines now that we have two visitors.

*** Method
All we need to do is change the main function to create multiple visitors, store them in a list, and cycle through the list of visitors when we process instructions.