add tag
3 years ago Skillmon

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):

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.

autumn_of_code-1.png

Top Answer
3 years ago Skillmon

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:

soc15-pyramid_dependency-1.png

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:

soc15-pyramid_dependency-1.png

3 years
Skillmon — Friday, 7th Oct 2022 17:14

Yay, I won the prize… 😃 The idea was that I don’t have to think of a new addition 😛

6 days
Skillmon — Saturday, 1st Oct 2022 20:12

The reason why I thought of this puzzle (as I said in the chat, it’s part of a problem I wanted to solve): Now I can create small calculation exercises for my daughters, like the following addition/subtraction pyramids 😃

rechenpyramide-1.png

4 hours
samcarter replying to Skillmon — Saturday, 1st Oct 2022 16:24

🤣

Skillmon replying to samcarter — Saturday, 1st Oct 2022 16:14

That photograph is obviously a fake, back then there was no colour film invented yet! This is the real image:

story_of_the_rabbit-1.png

an hour
samcarter — Saturday, 1st Oct 2022 14:56

@Skillmon, re: your answer,

Since we rabbits were the real builders of the pyramids, we of course know the answer!

I’ve heard a different version of the story:

11.png

a day
samcarter replying to Skillmon — Friday, 30th Sep 2022 09:10

That’s probably to do with animal protection and the minimal space requirements for their shelter 😃

an hour
Skillmon replying to samcarter — Friday, 30th Sep 2022 08:24

I also found a bug in ducksay when I created the placeholder (current release doesn’t work inside tabular)

12 hours
samcarter — Thursday, 29th Sep 2022 20:38

@Skillmon, re: your question, Fun puzzle! (and creative placeholder to hide the answers 😃 )

Enter question or answer id or url (and optionally further answer ids/urls from the same question) from

Separate each id/url with a space. No need to list your own answers; they will be imported automatically.