Build a three-weighing decision tree that finds the odd coin among 13. The type checker replays all 26 cases and rejects any plan that leaves a coin unnamed.
This one is not a type utility. It is a logic puzzle whose answer happens to be three object literals, and TypeScript is the referee.
Thirteen coins look identical. One of them is off: either heavier or lighter than the rest, and you do not know which way. You have a balance scale and three weighings. Fill in Weigh1, Weigh2 and Weigh3 so that every possible fake coin gets named. The starter ships a plan that only works if the fake coin is heavy, so the tests fail on the lighter half of the cases.
const Weigh1 = {
Left: [1, 2, 3, 4, 5, 6], Right: [7, 8, 9, 10, 11, 12]
} as const satisfies WeighingSetup1Weigh2 holds one setup per outcome of the first weighing, and Weigh3 holds one setup per pair of outcomes, so the three constants together are a nine-leaf decision tree.
You are given 13 coins. All of them weigh the same except for one, which is either heavier or lighter.
You also have a balance scale that allows you to compare the weights of coins on its two sides.
Your task is to determine which coin is the one with a different weight and whether it is heavier or lighter using no more than 3 weighings.
You will use the Weigh1, Weigh2, and Weigh3 constants to represent each
weighing step. Each constant contains the setup for the respective weighing
step based on the outcome of the previous weighing.
Additionally, the WeighingOutcome type will represent the result of each
weighing, indicating which side is heavier or if both sides are equal.
The test cases will call Weigh1, Weigh2, and Weigh3 to simulate the
weighing steps and verify the solution.
The printFakeCoin function demonstrates how to use these constants in real
JS runtime, performing real-time type validation on function parameters based
on complex logic.
For more insight into balance puzzles, you can refer to the Wikipedia article on balance puzzles: https://en.wikipedia.org/wiki/Balance_puzzle
View on GitHub: https://tsch.js.org/33473
Change the following code to make the test cases pass (no type check errors).
/* _____________ Your Code Here _____________ */
type Coins = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13
// Defines weighing setup to systematically compare two groups of coins
type WeighingSetup1 = {Left: readonly Coins[], Right: readonly Coins[]}
type WeighingSetup2 = {HeavierOnLeft: WeighingSetup1, HeavierOnRight: WeighingSetup1, EqualWeight: WeighingSetup1}
type WeighingSetup3 = {HeavierOnLeft: WeighingSetup2, HeavierOnRight: WeighingSetup2, EqualWeight: WeighingSetup2}
/**
* Below is the templates to get start
*/
const Weigh1 = {
Left: [1, 2, 3, 4, 5, 6], Right: [7, 8, Get access to all 200+ challenges, including every medium, hard, and extreme one.
One-time payment, lifetime access. 30-day money-back guarantee.
A plan that passes both assertions:
const Weigh1 = {
Left: [1, 2, 3, 4], Right: [5, 6, 7, 8]
} as const satisfies WeighingSetup1
const Weigh2 = {
HeavierOnLeft: {Left: [1, 2, 5], Right: [3, 4, 6]},
HeavierOnRight: {Left: [5, 6, 1], Right: [7, 8, 2]},
EqualWeight: {Left: [9, 10, 11], Right: [1, 2, 3]},
} as const satisfies WeighingSetup2
const Weigh3 = {
HeavierOnLeft: {
HeavierOnLeft: {Left: [1], Right: [2]},
HeavierOnRight: {Left: [3], Right: [4]},
EqualWeight: {Left: [7], Right: [8]}
},
HeavierOnRight: {
HeavierOnLeft: {Left: [5], Right: [6]},
HeavierOnRight: {Left: [7], Right: [8]},
EqualWeight: {Left: [3], Right: [4]}
},
EqualWeight: {
HeavierOnLeft: {Left: [9], Right: [10]},
HeavierOnRight: {Left: [9], Right: [10]},
EqualWeight: {Left: [12], Right: [1]}
}
} as const satisfies WeighingSetup3Three weighings give three outcomes each, so a plan has at most 27 distinguishable leaves. There are 26 possibilities: 13 coins times two directions. That looks comfortable until you notice the asymmetry in the second assertion. Thirteen coins cannot be fully solved, and the tests say so: FindUnidentifiedFakeCoins has to be never, but HasMoreCoinsThan<UnknownFakeCoinStatuses, 1> only has to be false. You must name the fake coin in all 26 cases, and you are allowed to leave at most one coin whose direction stays unknown.
That spare coin is the one that never touches the scale. If all three weighings balance, coin 13 is the only one left, and nothing in the record says whether it is heavy or light.
Six against six (the starter's split) spends too much on the first step. Four against four splits the problem the way the remaining budget wants it:
Note that both pans must hold the same number of coins. HasMoreCoinsThan in the test harness compares the two sides by counting union members, so a 4-against-3 setup tips before any coin weight matters and the weighing teaches you nothing.
The clever move in the unbalanced branch is mixing suspects across pans. After HeavierOnLeft, coins 1 to 4 are heavy-suspects and 5 to 8 are light-suspects. The setup {Left: [1, 2, 5], Right: [3, 4, 6]} puts two heavy-suspects and one light-suspect on each side:
// left heavier again -> 1 or 2 is heavy, or 6 is light
// right heavier -> 3 or 4 is heavy, or 5 is light
// balanced -> 7 or 8 is lightEach branch holds exactly three possibilities, which one final pairwise weighing resolves. In the balanced branch the trick is different: {Left: [9, 10, 11], Right: [1, 2, 3]} weighs three unknowns against three coins the first weighing already cleared, which converts "is it heavy or light" into a direction you can read straight off the scale.
Every leaf is one coin against one coin. Two of the three candidates sit on the pans and the third is named by elimination when the scale balances. {Left: [12], Right: [1]} is the only leaf that borrows a known-good coin again, because 12 arrives with no direction attached. If that last weighing also balances, coin 13 is the fake and its direction is the gap the tests permit.
as const satisfies is doing heresatisfies WeighingSetup1 checks each literal against the expected shape without widening it, so a stray Left: [14] is flagged where you wrote it instead of resurfacing later as a puzzling assertion failure. as const turns each pan into a readonly tuple rather than an array, and that is what keeps the harness honest: IsCoinsValid walks the tuple's numeric keys to spot a coin listed twice on the same pan. Drop as const and a pan has no numeric literal keys left to walk, so [1, 1, 5] slips past that check.
With both in place, typeof Weigh2['HeavierOnLeft']['Left'][number] is the union 1 | 2 | 5, and the harness does all its reasoning on those unions: Exclude to drop coins it has proven good, and a pass over {[C in Coins]: ...}[Coins] to replay all 13 coins against both directions.
Expect<Equal<UnidentifiedFakeCoins, never>> replays all 26 coin-and-direction pairs. A plan that assumes the fake coin is heavy, like the starter, leaves all 13 light cases unnamed.Expect<Equal<HasMoreCoinsThan<UnknownFakeCoinStatuses, 1>, false>> caps the undetermined directions at one, so you cannot buy an easy pass by leaving two coins off the scale.IsCoinsValid rejects a setup with a coin repeated inside one pan or present on both pans, so you cannot weigh coin 1 against itself to fake a balance.{Left: [], Right: []} leaves compile but identify nothing.printFakeCoin(Weigh1, Weigh2, Weigh3) call is the same check in argument position: while the plan is incomplete, the third parameter resolves to {error: `Unidentified: ${Coin}`} and the call stops type-checking, which is the error you see in the editor before the assertions even run.This challenge is originally from here.
Track your progress through 200+ hands-on challenges. Free, sign in with GitHub.
Or start solving right away: explore all TypeScript challenges