# Fish Riddle

The internet is full of distractions, and unfortunately, I am not always impervious to all of them. Some of them can lead to interesting results. Today, my distraction came in the shape of a riddle from a riddle from a TED-Ed video, which got me to explore my (rusty, but still somewhat present) math skills.

Now, I was excited to learn about the puzzle to see if I could use programming (I was thinking of a constraint solver or possibly just brute-forcing it) to solve it. But alas, it turns out that it’s just solvable with plain maths 🤷🏽‍♀️. So let’s dive in and see what we can do here.

## The problem

You can watch the video to get the story of the puzzle, but it breaks down like this: you have three quadrants, each with a number of fish tanks and sharks in it. You know how many there are in both the first and second quadrants, and you must find out how many there are in the third quadrant.

So let’s first introduce a number of variables to help is keep track of things. We define:

• $s_i$ as the number of creatures in sector $i$,
• $h_i$ as the number of sharks in sector $i$,
• $f_i$ as the number of fish in sector $i$,
• $t_i$ as the number of fish per tank in sector $i$,
• $n_i$ as the number of fish tanks in sector $i$.

## The constraints

There are six comstraints given in the puzzle.

1. There are 50 creatures in total, including sharks and fish.
1. Each sector has anywhere from one to seven sharks, with no two sectors having the same nmber of sharks.
1. Each tank has an equal number of fish. Since this is true, we will just use $t$ to refer to any of $t_i$, simce they are all the same.
1. In total, there are 13 or fewer fish tanks.
1. Sector Alpha has two sharks and four tanks.
1. Sector Beta has four sharks and two tanks.

## Solution

The objective for us is to find both the values of $t$, $h_3$ and $n_3$. To do this, I started out by using the given constraints to find the number of possible values for each of them.

Applying the given constraint #2, we can limit the search space for $h_3$ easily.

Similarly, using the given constraint #3, we can limit the search space for $n_3$ easily.

Finding out the search space for $t$ is unfortunately not that simple. First, we need to know how many creatures are currently accounted for.

From constraint 1, we know that there are 50 creatures. Thus, from the amount of creatures we have right now and from that, we can calculate how many are not accounted for yet, $r$ (for rest). We also know that the missing creatures must be in our sector (sector Gamma), so we have ourselves a nice simple equation.

Now, given this equation and knowing the search space for both $h_3$ and $n_3$, we can easily restrict the search space for $t$. If we pop in the maximum values for $h_3$ and $n_3$ and solve it, we can find the minimum value for $t$, and vice versa for the maximum value.

With this information, we can limit the search space for $t$, because we know that it must be within the bounds of $t_{min}$ and $t_{max}$.

Now, to actually solve this whole mess, we need to rearrange our equation a little bit.

With this equation, we can see that $44 - h_3$ must be divisible by both $(6 + n_3)$ and $t$. So, given that we have a list of candidates for $h_3$, we can simply check their divisors and see if any of them are candidates for $t$.

$h_3$ equation $\{x \in \mathbb{N} \vert x \mid (44 - h_3), t_{min} \leq x \leq t_{max}\}$
$1$ $43 = (6 + n_3) t$ $\{\}$
$3$ $41 = (6 + n_3) t$ $\{\}$
$5$ $39 = (6 + n_3) t$ $\{3\}$
$6$ $38 = (6 + n_3) t$ $\{\}$
$7$ $37 = (6 + n_3) t$ $\{\}$

Seeing that only $h_3 = 5$ produced a valid $t = 3$, these must be our values. Now, all that is left to do is pop them right back into the equation to find $n_3$.

There we go, $n_3 = 7$. This means that in sector Gamma, there are five sharks and seven fish tanks. Every fish tank contains three fish.

Too bad that this could be solved on paper, I’m hoping that next time I can finally get an excuse to play around with a fancy constraint solver or implement something. But in the meantime, it was fun to do and I hope I didn’t get anything wrong.