Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Does this addition pyramid puzzle have a unique solution?

Given an addition pyramid \$P\$, determine whether it can be solved. An addition pyramid consists of layers, each having one number less than the one below it. Layer \$i\$ is symbolized as \$P_i\$. \$P_1\$ is the base layer, and \$P_{i+1}\$ is the layer atop \$P_i\$. The \$j\$th number of \$P_i\$ is denoted as \$P_{i,j}\$. \$P_{i,1}\$ is the leftmost number of \$P_i\$, and \$P_{i,j+1}\$ is the number to the right of \$P_{i,j}\$. You may visualize \$P_{i+1,j}\$ residing on top of \$P_{i,j}\$ and \$P_{i,j+1}\$ at the middle, hence the name "addition pyramid".

  • \$\forall P_{i,j},P_{i,j}\in\mathbb N^*\$, that is, every number in the pyramid is a non-zero positive integer.
  • \$\forall i>1,P_{i,j}=P_{i-1,j}+P_{i-1,j+1}\$, that is, every number not on the base layer of the pyramid is the sum of the two numbers below it.
  • If \$P_1\$ has \$n\$ numbers, \$P_i\$ has \$n-i+1\$ numbers, therefore \$P_{i,n-i+1}\$ is the rightmost number of \$P_i\$. In simpler terms, each layer has one number less than the layer below it.

An addition pyramid puzzle \$Q\$ is an addition pyramid with some numbers removed (replaced with \$?\$). Its solution is an addition pyramid \$P\$, where \$\forall Q_{i,j}\ne{?},P_{i,j}=Q_{i,j}\$, that is, the numbers that were originally present in the puzzle have been left unchanged. Such a puzzle may have more than one solution.

Your job is, given an addition pyramid puzzle, to determine if it has exactly one solution.

Input

You can get input in any of the following forms, but be consistent:

  • Array of layers.
  • Array of layers, shaped like a pyramid using a consistent non-positive-integer value as a separator between elements (used only once each time) as well as left and right padding. The separator and the padding must be the same.
  • Array of layers with a consistent valid right or left padding (you must be consistent and not mix right and left padding in this case).

Please note that a consistent value that's not a strictly positive integer must be used to represent a missing number; this value can't be used as padding. Also, you can take the layers concatenated (you can still separate them), and the ordering can either be from the base to the top or from the top to the base.

Output

One of two consistent distinct values, where one represents the presence of a unique solution and the other the absence of a solution or the presence of more than one solution.

Rules

  • \$Q_{i+1,j}=Q_{i,j}+Q_{i,j+1}\$ will always be true if \$Q_{i,j},Q_{i,j+1},Q_{i+1,j}\in\mathbb N^*\$, that is, the input is guaranteed to not contain a number on top of two other numbers that isn't their sum if all three numbers are known.
  • \$\exists Q_{i,j},Q_{i,j}\ne{?}\$, that is, the pyramid will contain at least one known number.
  • Don't do these things.
  • This is , so the shortest answer wins! However, don't let that discourage you from posting a solution just because your language is "too verbose".

Test cases

An array with the layers from the top to the base is used for these test cases, with 0 representing \$?\$.

[[10], [0, 0], [0, 2, 0], [0, 0, 0, 1]] -> True
[[32], [0, 0], [0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]] -> True
[[0], [1, 1]] -> True
[[1], [0, 0]] -> False
[[10], [5, 5], [2, 3, 2], [0, 0, 0, 0]] -> False
[[5], [0, 0], [0, 0, 0]] -> False

Worked examples

The test cases are worked here.

Unique solution 1

$$\begin{array}c&&&10\\&&?&&?\\&?&&2&&?\\?&&?&&?&&1\end{array}$$

Step 1: \$x+y=2\leftrightarrow x=y=1\$.

$$\begin{array}c&&&10\\&&?&&?\\&?&&2&&?\\?&&\color{red}1&&\color{red}1&&1\end{array}$$

Step 2: \$x=y=1\leftrightarrow x+y=2\$.

$$\begin{array}c&&&10\\&&?&&?\\&?&&2&&\color{red}2\\?&&1&&1&&1\end{array}$$

Step 3: \$x=y=2\rightarrow x+y=4\$.

$$\begin{array}c&&&10\\&&?&&\color{red}4\\&?&&2&&2\\?&&1&&1&&1\end{array}$$

Step 4: \$x+4=10\leftrightarrow x=6\$.

$$\begin{array}c&&&10\\&&\color{red}6&&4\\&?&&2&&2\\?&&1&&1&&1\end{array}$$

Steps 5-6 are similar to 4.

$$\begin{array}c&&&10\\&&6&&4\\&\color{red}4&&2&&2\\\color{red}3&&1&&1&&1\end{array}$$

So here we have our unique solution.

Unique solution 2

$$\begin{array}c&&&&&32\\&&&&?&&?\\&&&?&&?&&?\\&&?&&?&&?&&?\\&?&&?&&?&&?&&?\\?&&?&&?&&?&&?&&?\end{array}$$

Step 1: There's no obvious approach here, so let's try using the minimum possible values.

$$\begin{array}c&&&&&32\\&&&&?&&?\\&&&?&&?&&?\\&&?&&?&&?&&?\\&?&&?&&?&&?&&?\\\color{red}1&&\color{red}1&&\color{red}1&&\color{red}1&&\color{red}1&&\color{red}1\end{array}$$

Steps 2-5: It looks like the minimum values result in a solution, therefore this is the only solution and is therefore unique.

$$\begin{array}c&&&&&32\\&&&&\color{red}{16}&&\color{red}{16}\\&&&\color{red}8&&\color{red}8&&\color{red}8\\&&\color{red}4&&\color{red}4&&\color{red}4&&\color{red}4\\&\color{red}2&&\color{red}2&&\color{red}2&&\color{red}2&&\color{red}2\1円&&1&&1&&1&&1&&1\end{array}$$

Hint: There's a theorem about addition pyramid puzzles related to this puzzle that you can prove if you think hard enough.

Unique solution 3

$$\begin{array}c&?\1円&&1\end{array}$$

Step 1: \$x=y=1\rightarrow x+y=2\$.

$$\begin{array}c&\color{red}2\1円&&1\end{array}$$

This is an obviously unique solution.

No solution 1

$$\begin{array}c&1\\?&&?\end{array}$$

\$\min\mathbb N^*=1\rightarrow x,y\ge1\rightarrow x+y\ge2>1\$, so there is no solution.

No solution 2

$$\begin{array}c&&&10\\&&5&&5\\&2&&3&&2\\?&&?&&?&&?\end{array}$$

Steps 1-2: \$x+y=2\leftrightarrow x=y=1\$.

$$\begin{array}c&&&10\\&&5&&5\\&2&&3&&2\\\color{red}1&&\color{red}1&&\color{red}1&&\color{red}1\end{array}$$

From there, it follows that \1ドル+1=3\$, which is a contradiction, therefore there's no solution.

Non-unique solution

$$\begin{array}c&&5\\&?&&?\\?&&?&&?\end{array}$$

Two solutions:

$$\begin{array}c&&5&&&&&&&5\\&2&&3&&&&&3&&2\1円&&1&&2&&&2&&1&&1\end{array}$$

Since there are at least two solutions, there's no unique solution.

Answer*

Draft saved
Draft discarded
Cancel
6
  • \$\begingroup\$ Very nice. How does the "generate all possibilties" logic work? \$\endgroup\$ Commented Jul 30, 2019 at 21:08
  • 1
    \$\begingroup\$ @Jonah the Catrtesian power of the max number in the grid with the length of the base. e.g. if The maximum number were 10 and the length of the base 4 then it would test everything from [1,1,1,1] to [10,10,10,10], i.e. 10000 possibilities. \$\endgroup\$ Commented Jul 30, 2019 at 21:22
  • \$\begingroup\$ Outputs truthy for [[0,0],[0]]. \$\endgroup\$ Commented Jul 31, 2019 at 12:53
  • \$\begingroup\$ @KevinCruijssen I’ve asked for clarification whether input with no known values is valid. If so, I can change to »2 which also has the advantage of regaining the efficiency lost with my last change, albeit at the cost of a byte. \$\endgroup\$ Commented Jul 31, 2019 at 13:03
  • 2
    \$\begingroup\$ ...Ƭ€Ṗ€a@ċ=1 saves two bytes (unless there are edge cases with the AND not catered for by the tests?) \$\endgroup\$ Commented Jul 31, 2019 at 18:15

AltStyle によって変換されたページ (->オリジナル) /