In a certain video game, it is desirable to maximize the occurrence of crafting an item $B$, which depends on possessing the proper quantities of material. To construct 1 $B$ one needs 1 of ingredient $L$ and 2 of ingredient $S$. In addition, one can convert 1 $L$ to 4 $S$. Given this information, my goal is to determine a number $T$ of $L$ to convert to $S$. For example, if I had 3 $L$, the optimal $T$ would be 1, since then the ratio between $S$ and $L$ would be exactly 2, as desired. If I started with $l$ initial $L$ and $s$ initial $S$ (with all integer variables), then the relationship should be as follows:
$$2L = S \implies\\ 2(l-T)=s+4T \\ 2l-2T=s+4T \\ 2l-s=6T \\ T=\frac{2l-s}{6}
$$
I ran into some trouble refining this, because it returns non-integers for some values which are useless in-game. I've tested
$$\begin{equation}T=\left\|\frac{2l-s}{6}\right\|\end{equation}$$
with an iterative process and determined through trial that this formula returns the correct result for $l,s \in \{1,2,3,4\}$. This doesn't seem to be a rigorous or complete solution. In addition, this formula fails when $l=1$ and $s=6$, and possibly elsewhere.
I've supposed that this fails when a negative result is generated, because one cannot convert backwards (although the sentiment is nice.) Given the rounding (unless I improperly evaluate $\|x\|$), this occurs when
$$\frac{2l-s}{6} \leq \frac{-1}{2} \\ 2l-s \leq -3 \\ 2l \leq s-3 \\ 2l+3 \leq s$$
This formula could then be represented as
$$T(l,s)= \begin{cases}
0 & 2l+3\leq s \\
\left\|\frac{2l-s}{6}\right\| & 2l+3>s
\end{cases}
$$
This feels inelegant and non-rigorous. Is there a more general formula for this problem? Also, how could I make the conclusions about the rounding more rigorous? I have the same question for my definition of the cases in which the main formula fails, i.e. when $T=0\neq \|(2l-s)/6\|$.
Edit: I'm considering testing the final equation for all feasible values of $l$ and $s$, i.e. the in-game inventory limits.
We start the game having $l$ units of material $L$ and $s$ units of $S$. After converting $T$ items of $L$ we have $l-T$ items of $L$ left and $s+4T$ items of $S$. With all this material we can obtain $\min\left(l-T,\frac{s+4T}2\right)$ of $B$. Since crafting $B$ is the only goal we want every item of $L$ to be used. Thus we have the equality: $$l-T=\min\left(l-T,\frac{s+4T}2\right)$$ It is known than $\min(x,y)=\frac{x+y-|x-y|}2$. So we have $$l-T=\frac{l+\frac{s}2+T-\left|l-\frac{s}2-3T\right|}2$$ which simplifies to $$\left|l-\frac{s}2-3T\right|=3T+\frac{s}2-l$$ This means that $$l-\frac{s}2-3T\leq0$$ or $$T\geq\frac{2l-s}6$$ And since $T\in\mathbb{N}_0$ we get $$T=\max\left(0;\left\lfloor\frac{2l-s}6\right\rfloor+1\right)$$ where $\lfloor\cdot\rfloor$ is floor function.