5 \[Given\] a sequence of digits (your puzzle input) and find the sum of all digits that match the next digit in the list. The list is circular, so the digit after the last digit is the first digit in the list.
9 * 1122 produces a sum of 3 (1 + 2) because the first digit (1) matches the second digit and the third digit (2) matches the fourth digit.
10 * 1111 produces 4 because each digit (all 1) matches the next.
11 * 1234 produces 0 because no digit matches the next.
12 * 91212129 produces 9 because the only digit that matches the next one is the last digit, 9.
16 from notebook_preamble import J, V, define
19 I'll assume the input is a Joy sequence of integers (as opposed to a string or something else.)
21 We might proceed by creating a word that makes a copy of the sequence with the first item moved to the last, and zips it with the original to make a list of pairs, and a another word that adds (one of) each pair to a total if the pair matches.
23 AoC2017.1 == pair_up total_matches
25 Let's derive `pair_up`:
28 -------------------------
32 Straightforward (although the order of each pair is reversed, due to the way `zip` works, but it doesn't matter for this program):
35 [a b c] [a b c] uncons swap
36 [a b c] [b c] a unit concat
42 define('pair_up dup uncons swap unit concat zip')
55 J('[1 2 2 3] pair_up')
58 [[2 1] [2 2] [3 2] [1 3]]
61 Now we need to derive `total_matches`. It will be a `step` function:
63 total_matches == 0 swap [F] step
65 Where `F` will have the pair to work with, and it will basically be a `branch` or `ifte`.
69 It will probably be easier to write if we dequote the pair:
72 ----------------------
75 Now `F′` becomes just:
77 total n m [=] [pop +] [popop] ifte
81 F == i [=] [pop +] [popop] ifte
85 total_matches == 0 swap [i [=] [pop +] [popop] ifte] step
89 define('total_matches 0 swap [i [=] [pop +] [popop] ifte] step')
94 J('[1 2 3] pair_up total_matches')
102 J('[1 2 2 3] pair_up total_matches')
108 Now we can define our main program and evaluate it on the examples.
112 define('AoC2017.1 pair_up total_matches')
117 J('[1 1 2 2] AoC2017.1')
125 J('[1 1 1 1] AoC2017.1')
133 J('[1 2 3 4] AoC2017.1')
141 J('[9 1 2 1 2 1 2 9] AoC2017.1')
149 J('[9 1 2 1 2 1 2 9] AoC2017.1')
155 pair_up == dup uncons swap unit concat zip
156 total_matches == 0 swap [i [=] [pop +] [popop] ifte] step
158 AoC2017.1 == pair_up total_matches
165 Now the paired digit is "halfway" round.
167 [a b c d] dup size 2 / [drop] [take reverse] cleave concat zip
171 J('[1 2 3 4] dup size 2 / [drop] [take reverse] cleave concat zip')
174 [[3 1] [4 2] [1 3] [2 4]]
177 I realized that each pair is repeated...
181 J('[1 2 3 4] dup size 2 / [drop] [take reverse] cleave zip')
184 [1 2 3 4] [[1 3] [2 4]]
189 define('AoC2017.1.extra dup size 2 / [drop] [take reverse] cleave zip swap pop total_matches 2 *')
194 J('[1 2 1 2] AoC2017.1.extra')
202 J('[1 2 2 1] AoC2017.1.extra')
210 J('[1 2 3 4 2 5] AoC2017.1.extra')
218 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...
220 rotate_seq == uncons swap unit concat
221 pair_up == dup rotate_seq zip
222 add_if_match == [=] [pop +] [popop] ifte
223 total_matches == [i add_if_match] step_zero
225 AoC2017.1 == pair_up total_matches
227 half_of_size == dup size 2 /
228 split_at == [drop] [take reverse] cleave
229 pair_up.extra == half_of_size split_at zip swap pop
231 AoC2017.1.extra == pair_up.extra total_matches 2 *