Rebuild docs
This commit is contained in:
@@ -21,7 +21,7 @@ For example:
|
||||
|
||||
from notebook_preamble import J, V, define
|
||||
|
||||
I'll assume the input is a Joy sequence of integers (as opposed to a
|
||||
I’ll assume the input is a Joy sequence of integers (as opposed to a
|
||||
string or something else.)
|
||||
|
||||
We might proceed by creating a word that makes a copy of the sequence
|
||||
@@ -31,26 +31,26 @@ a total if the pair matches.
|
||||
|
||||
::
|
||||
|
||||
AoC2017.1 == pair_up total_matches
|
||||
AoC2017.1 == pair_up total_matches
|
||||
|
||||
Let's derive ``pair_up``:
|
||||
Let’s derive ``pair_up``:
|
||||
|
||||
::
|
||||
|
||||
[a b c] pair_up
|
||||
-------------------------
|
||||
[[a b] [b c] [c a]]
|
||||
[a b c] pair_up
|
||||
-------------------------
|
||||
[[a b] [b c] [c a]]
|
||||
|
||||
Straightforward (although the order of each pair is reversed, due to the
|
||||
way ``zip`` works, but it doesn't matter for this program):
|
||||
way ``zip`` works, but it doesn’t matter for this program):
|
||||
|
||||
::
|
||||
|
||||
[a b c] dup
|
||||
[a b c] [a b c] uncons swap
|
||||
[a b c] [b c] a unit concat
|
||||
[a b c] [b c a] zip
|
||||
[[b a] [c b] [a c]]
|
||||
[a b c] dup
|
||||
[a b c] [a b c] uncons swap
|
||||
[a b c] [b c] a unit concat
|
||||
[a b c] [b c a] zip
|
||||
[[b a] [c b] [a c]]
|
||||
|
||||
.. code:: ipython2
|
||||
|
||||
@@ -80,40 +80,40 @@ Now we need to derive ``total_matches``. It will be a ``step`` function:
|
||||
|
||||
::
|
||||
|
||||
total_matches == 0 swap [F] step
|
||||
total_matches == 0 swap [F] step
|
||||
|
||||
Where ``F`` will have the pair to work with, and it will basically be a
|
||||
``branch`` or ``ifte``.
|
||||
|
||||
::
|
||||
|
||||
total [n m] F
|
||||
total [n m] F
|
||||
|
||||
It will probably be easier to write if we dequote the pair:
|
||||
|
||||
::
|
||||
|
||||
total [n m] i F′
|
||||
----------------------
|
||||
total n m F′
|
||||
total [n m] i F′
|
||||
----------------------
|
||||
total n m F′
|
||||
|
||||
Now ``F′`` becomes just:
|
||||
|
||||
::
|
||||
|
||||
total n m [=] [pop +] [popop] ifte
|
||||
total n m [=] [pop +] [popop] ifte
|
||||
|
||||
So:
|
||||
|
||||
::
|
||||
|
||||
F == i [=] [pop +] [popop] ifte
|
||||
F == i [=] [pop +] [popop] ifte
|
||||
|
||||
And thus:
|
||||
|
||||
::
|
||||
|
||||
total_matches == 0 swap [i [=] [pop +] [popop] ifte] step
|
||||
total_matches == 0 swap [i [=] [pop +] [popop] ifte] step
|
||||
|
||||
.. code:: ipython2
|
||||
|
||||
@@ -197,17 +197,17 @@ Now we can define our main program and evaluate it on the examples.
|
||||
|
||||
::
|
||||
|
||||
pair_up == dup uncons swap unit concat zip
|
||||
total_matches == 0 swap [i [=] [pop +] [popop] ifte] step
|
||||
pair_up == dup uncons swap unit concat zip
|
||||
total_matches == 0 swap [i [=] [pop +] [popop] ifte] step
|
||||
|
||||
AoC2017.1 == pair_up total_matches
|
||||
AoC2017.1 == pair_up total_matches
|
||||
|
||||
|
||||
Now the paired digit is "halfway" round.
|
||||
Now the paired digit is “halfway” round.
|
||||
|
||||
::
|
||||
|
||||
[a b c d] dup size 2 / [drop] [take reverse] cleave concat zip
|
||||
[a b c d] dup size 2 / [drop] [take reverse] cleave concat zip
|
||||
|
||||
.. code:: ipython2
|
||||
|
||||
@@ -219,7 +219,7 @@ Now the paired digit is "halfway" round.
|
||||
[[3 1] [4 2] [1 3] [2 4]]
|
||||
|
||||
|
||||
I realized that each pair is repeated...
|
||||
I realized that each pair is repeated…
|
||||
|
||||
.. code:: ipython2
|
||||
|
||||
@@ -270,19 +270,19 @@ Refactor FTW
|
||||
|
||||
With Joy a great deal of the heuristics from Forth programming carry
|
||||
over nicely. For example, refactoring into small, well-scoped commands
|
||||
with mnemonic names...
|
||||
with mnemonic names…
|
||||
|
||||
::
|
||||
|
||||
rotate_seq == uncons swap unit concat
|
||||
pair_up == dup rotate_seq zip
|
||||
add_if_match == [=] [pop +] [popop] ifte
|
||||
total_matches == [i add_if_match] step_zero
|
||||
rotate_seq == uncons swap unit concat
|
||||
pair_up == dup rotate_seq zip
|
||||
add_if_match == [=] [pop +] [popop] ifte
|
||||
total_matches == [i add_if_match] step_zero
|
||||
|
||||
AoC2017.1 == pair_up total_matches
|
||||
AoC2017.1 == pair_up total_matches
|
||||
|
||||
half_of_size == dup size 2 /
|
||||
split_at == [drop] [take reverse] cleave
|
||||
pair_up.extra == half_of_size split_at zip swap pop
|
||||
half_of_size == dup size 2 /
|
||||
split_at == [drop] [take reverse] cleave
|
||||
pair_up.extra == half_of_size split_at zip swap pop
|
||||
|
||||
AoC2017.1.extra == pair_up.extra total_matches 2 *
|
||||
AoC2017.1.extra == pair_up.extra total_matches 2 *
|
||||
|
||||
Reference in New Issue
Block a user