Update some of the docs.
This commit is contained in:
@@ -17,11 +17,11 @@ For example:
|
||||
- 91212129 produces 9 because the only digit that matches the next one
|
||||
is the last digit, 9.
|
||||
|
||||
.. code:: ipython2
|
||||
.. code:: ipython3
|
||||
|
||||
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,32 +31,32 @@ 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
|
||||
.. code:: ipython3
|
||||
|
||||
define('pair_up == dup uncons swap unit concat zip')
|
||||
define('pair_up dup uncons swap unit concat zip')
|
||||
|
||||
.. code:: ipython2
|
||||
.. code:: ipython3
|
||||
|
||||
J('[1 2 3] pair_up')
|
||||
|
||||
@@ -66,7 +66,7 @@ way ``zip`` works, but it doesn’t matter for this program):
|
||||
[[2 1] [3 2] [1 3]]
|
||||
|
||||
|
||||
.. code:: ipython2
|
||||
.. code:: ipython3
|
||||
|
||||
J('[1 2 2 3] pair_up')
|
||||
|
||||
@@ -80,46 +80,46 @@ 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
|
||||
.. code:: ipython3
|
||||
|
||||
define('total_matches == 0 swap [i [=] [pop +] [popop] ifte] step')
|
||||
define('total_matches 0 swap [i [=] [pop +] [popop] ifte] step')
|
||||
|
||||
.. code:: ipython2
|
||||
.. code:: ipython3
|
||||
|
||||
J('[1 2 3] pair_up total_matches')
|
||||
|
||||
@@ -129,7 +129,7 @@ And thus:
|
||||
0
|
||||
|
||||
|
||||
.. code:: ipython2
|
||||
.. code:: ipython3
|
||||
|
||||
J('[1 2 2 3] pair_up total_matches')
|
||||
|
||||
@@ -141,11 +141,11 @@ And thus:
|
||||
|
||||
Now we can define our main program and evaluate it on the examples.
|
||||
|
||||
.. code:: ipython2
|
||||
.. code:: ipython3
|
||||
|
||||
define('AoC2017.1 == pair_up total_matches')
|
||||
define('AoC2017.1 pair_up total_matches')
|
||||
|
||||
.. code:: ipython2
|
||||
.. code:: ipython3
|
||||
|
||||
J('[1 1 2 2] AoC2017.1')
|
||||
|
||||
@@ -155,7 +155,7 @@ Now we can define our main program and evaluate it on the examples.
|
||||
3
|
||||
|
||||
|
||||
.. code:: ipython2
|
||||
.. code:: ipython3
|
||||
|
||||
J('[1 1 1 1] AoC2017.1')
|
||||
|
||||
@@ -165,7 +165,7 @@ Now we can define our main program and evaluate it on the examples.
|
||||
4
|
||||
|
||||
|
||||
.. code:: ipython2
|
||||
.. code:: ipython3
|
||||
|
||||
J('[1 2 3 4] AoC2017.1')
|
||||
|
||||
@@ -175,7 +175,7 @@ Now we can define our main program and evaluate it on the examples.
|
||||
0
|
||||
|
||||
|
||||
.. code:: ipython2
|
||||
.. code:: ipython3
|
||||
|
||||
J('[9 1 2 1 2 1 2 9] AoC2017.1')
|
||||
|
||||
@@ -185,7 +185,7 @@ Now we can define our main program and evaluate it on the examples.
|
||||
9
|
||||
|
||||
|
||||
.. code:: ipython2
|
||||
.. code:: ipython3
|
||||
|
||||
J('[9 1 2 1 2 1 2 9] AoC2017.1')
|
||||
|
||||
@@ -197,19 +197,19 @@ 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
|
||||
.. code:: ipython3
|
||||
|
||||
J('[1 2 3 4] dup size 2 / [drop] [take reverse] cleave concat zip')
|
||||
|
||||
@@ -219,9 +219,9 @@ 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
|
||||
.. code:: ipython3
|
||||
|
||||
J('[1 2 3 4] dup size 2 / [drop] [take reverse] cleave zip')
|
||||
|
||||
@@ -231,11 +231,11 @@ I realized that each pair is repeated…
|
||||
[1 2 3 4] [[1 3] [2 4]]
|
||||
|
||||
|
||||
.. code:: ipython2
|
||||
.. code:: ipython3
|
||||
|
||||
define('AoC2017.1.extra == dup size 2 / [drop] [take reverse] cleave zip swap pop total_matches 2 *')
|
||||
define('AoC2017.1.extra dup size 2 / [drop] [take reverse] cleave zip swap pop total_matches 2 *')
|
||||
|
||||
.. code:: ipython2
|
||||
.. code:: ipython3
|
||||
|
||||
J('[1 2 1 2] AoC2017.1.extra')
|
||||
|
||||
@@ -245,7 +245,7 @@ I realized that each pair is repeated…
|
||||
6
|
||||
|
||||
|
||||
.. code:: ipython2
|
||||
.. code:: ipython3
|
||||
|
||||
J('[1 2 2 1] AoC2017.1.extra')
|
||||
|
||||
@@ -255,7 +255,7 @@ I realized that each pair is repeated…
|
||||
0
|
||||
|
||||
|
||||
.. code:: ipython2
|
||||
.. code:: ipython3
|
||||
|
||||
J('[1 2 3 4 2 5] AoC2017.1.extra')
|
||||
|
||||
@@ -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