This is (an unofficial) part of the Summer of Code 2022 series, see https://topanswers.xyz/tex?q=2059 for more information
We want to build a small 2D pyramid. A pyramid has n stories with the first story having n stones, the second having n-1 stones, and so forth until the nth story, having 1 stone. The stones are numbered, starting from the bottom left like in the following 3-story pyramid (beware the glorious ASCII-art):
xxxxxxxxxx
_______
| |
| 6 |
___|_______|___
| | |
| 4 | 5 |
___|_______|_______|___
| | | |
| 1 | 2 | 3 |
|_______|_______|_______|
Your task: Given a sequence of already placed stones in the form ,x,y,z,
(so a comma separated list with a leading comma, might have more or less than 3 indices) with x
, y
, and z
being different stone-numbers, and another stone-number (which is not in the sequence), decide if the stone has a dependent form.
A stone shall have a dependent form if either the two stones below it are already in the sequence (so for instance 4 is dependent if 1 and 2 are part of the sequence in our 3-story pyramid), or the stone above it and the other stone below the above stone are already part of the sequence (so for instance 3 is dependent if 5 and 2 are part of the sequence in our 3-story pyramid).
Your code should work for arbitrary pyramid heights (within TeX-limitations).
Bonus question: A stone shall also be considered dependent if the two stones below it are already dependent, or the stone above it and the other stone below the above stone are already dependent. For the bonus question you might also assume that the input sequence does not contain redundant information, so no later element of the sequence is dependent on earlier sequence items.
EDIT: Of course you also know the pyramid height if you need to as an argument (or inside a macro or however you want to get that information).
EDIT 2: There is a prize! The one with the most starred answer by Friday, 2022-10-07 00:00 UTC, can make a wish for a new animal to be added to ducksay
. If two answers are tight for the most stars the early bird catches the worm.
Since we rabbits were the real builders of the pyramids, we of course know the answer!
Base Puzzle
The following solves the normal (not the bonus) puzzle. But see the next section [Bonus Puzzle] below. This simply builds a formatted list of stones which the current stone might depend on and then iterates through said list to look for a combination of which both stones appear in the list.
Since I was lazy I didn’t implement the search whether a stone is in the list expandably, and hence this is not fully expandable (though it is for the most part, only the search function isn’t – and the assignment for the list, which I could drop if I implemented an expandable search function).
I hope the comments are enough to understand the code.
This prints:
Bonus Puzzle
This solves the bonus puzzle. Our approach is a bit different than the one above. Instead of checking the possible neighbour pairs being in the list we first build a list of all dependent stones (which we append to our input list). For that we need to nest several loops, again I hope the comments are sufficient to understand the code 😛
Output: