2022-03-22

|~4 min read

|617 words

I’m out on vacation with my family and we were playing a card game, *Tiny Polka Dot*, which is described as “Number-Loving Learning Fun”.

It’s full of little puzzles to solve using dots and numbers. One, named Pyramid Puzzle 2, is described as suitable for ages 7+. Bull sh*t.

The puzzle: Arrange the ten cards (all with a number 1 through 10 on them) to make a pyramid so that each number is the difference of the two below it.

The card provides an example:

```
4
5 9
```

We weren’t having any luck intuiting our way through the problem, but fortunately, I had a computer on hand so we wrote it up and brute forced our way.

The key was to be able to describe our triangle as a system of interrelated equations.

```
a = Math.abs(b-c)
b = Math.abs(d-e)
c = Math.abs(e-f)
d = Math.abs(g-h)
e = Math.abs(h-i)
f = Math.abs(j-i)
```

Where `a`

represents the top of the pyramid, `b`

and `c`

are the second row, etc.

Once we had that, we could set up a loop and let the computer just try different combinations until it arrived at a suitable match. Please note it may take a few (hundred thousand / million) tries.

Here’s an interactive repl and the full solution below.

pyramidPuzzle2.js

```
function allowedRand() {
return Math.ceil(Math.random() * 10)
}
function pyramidPuzzle2() {
let a, b, c, d, e, f, g, h, i, j
let count = 1
while (true) {
a = b = c = d = e = f = g = h = i = j = allowedRand()
count += 1
console.log({ count })
while (b == a) {
b = allowedRand()
}
while (c == a || c == b) {
c = allowedRand()
}
while (d == c || d == b || d == a) {
d = allowedRand()
}
while (e == d || e == c || e == b || e == a) {
e = allowedRand()
}
while (f == e || f == d || f == c || f == b || f == a) {
f = allowedRand()
}
while (g == f || g == e || g == d || g == c || g == b || g == a) {
g = allowedRand()
}
while (h == g || h == f || h == e || h == d || h == c || h == b || h == a) {
h = allowedRand()
}
while (
i == h ||
i == g ||
i == f ||
i == e ||
i == d ||
i == c ||
i == b ||
i == a
) {
i = allowedRand()
}
while (
j == i ||
j == h ||
j == g ||
j == f ||
j == e ||
j == d ||
j == c ||
j == b ||
j == a
) {
j = allowedRand()
}
if (
f === Math.abs(j - i) &&
e === Math.abs(h - i) &&
d === Math.abs(g - h) &&
c === Math.abs(e - f) &&
b === Math.abs(d - e) &&
a === Math.abs(b - c)
) {
break
}
}
console.log(`${a}\n${b}, ${c}\n${d}, ${e}, ${f}\n${g}, ${h}, ${i}, ${j}`)
}
pyramidPuzzle2()
```

One fun constraint that you can add is to limit the top two numbers to three or four:

```
while (a !== 3 && a !== 4) {
a = allowedRand()
}
```

*Why* this is a constraint, I’m not totally sure (a friend suggested it), but the number of permutations I needed to run through was cut by ~90% once added.

Hi there and thanks for reading! My name's Stephen. I live in Chicago with my wife, Kate, and dog, Finn. Want more? See about and get in touch!