import { html } from"@observablehq/inputs"// Sliders and labels// CalculationsW = (1- R) * X * Pt;Pb = Pt / X;Db =100;Dt =100* (W - Pb) / Pt;// Catppuccin Macchiato base = #24273A; text = #CAD3F5html`<div style=" position: fixed; bottom: 2em; left: 2em; z-index: 1000; background: #24273A; color: #CAD3F5; border-radius: 1em; padding: 1em 1.5em; box-shadow: 0 2px 12px rgba(0,0,0,0.10); min-width: 240px; max-width: 340px;"> <h3 style="margin-top:0">Controls</h3>${viewof R}<br>${viewof X}<br>${viewof Pt}<br><table> <thead> <tr><th>Variable</th><th>Value</th></tr> </thead> <tbody> <tr><td>Number of workers at the bread factory(Lb)</td><td>${Lb.toFixed(3)}</td></tr> <tr><td>Number of workers at the toy factory(Lt)</td><td>${Lt.toFixed(3)}</td></tr> <tr><td>Amount of bread produced (Qb)</td><td>${Qb.toFixed(3)}</td></tr> <tr><td>Number of toys produced (Qt)</td><td>${Qt.toFixed(3)}</td></tr> <tr><td>Wage (W)</td><td>${W.toFixed(3)}</td></tr> <tr><td>Price of bread (Pb)</td><td>${Pb.toFixed(3)}</td></tr> <tr><td>Demand for bread (Db)</td><td>${Db.toFixed(3)}</td></tr> <tr><td>Demand for toys (Dt)</td><td>${Dt.toFixed(3)}</td></tr> </tbody></table></div>`
Blair wakes before dawn and looks out their window. The gray valley is still shrouded in mist, but already the villagers of Northvein are stirring. They look up to the cloud-covered mountain casting a shadow over the valley and hear the rumble of the dragon sleeping within.
The dragon has looked after the valley for as long as history remembers and has gifted the villagers two factories, one for bread and one for toys. All it asks for in return is some of the gold made from the sale of these goods, which is hauled up the mountainside once a month.
Blair watches the villagers walk towards the factories at the edge of the village, troves of people in their gray clothing hobbling along the cobblestone streets. Some stop at the shrine in the middle of town, pausing for a moment to pray to the moon goddess who watches over the valley—some for good fortune, some for good wages, and some for guidance as to which factory to work at today.
All the villagers — including Blair — don’t have fixed jobs. Instead, they wake up each morning and choose which factory to go to. They are all equally skilled at making toys as they are at making bread, so the decision for some can be quite difficult. Some toss coins, some look at tea leaves, and some pray for guidance. But Blair — who was partial to a coin flip — wanted to try something new.
How much Can I Earn?
Blair started thinking the previous evening, on their walk back from the market, that surly there is a best way to pick out which factory to go to. The inhabitants of Northvein take little into account when picking which factory to go to besides the weight of their purses at the end of the day. The way that income is determined in the village is by equally dividing the profits for the day among the workers in each factory, after, of course, the dragon has taken their share of the earnings.
Blair pulls out a notepad and sits down at a table by the windowsill. They write, in scrawly handwriting:
\[
W = (1-R)X \cdot P
\]
Noting in their head \(W\) is the wage, \(R\) is the cut the dragon takes, \(X\) is the number of toys or loaves of bread they can make in a day, and \(P\) is how much gold the goods can fetch at the market.
Doing the maths in their head, they quickly take a guess as to how much they might earn today.
As the streams of people outside their window started to thin, Blair realised that to work out how much they could earn, they needed to work out how much gold could be fetched for toys and bread at the market. They packed their notepad into their bag and flipped the coin in their pocket; heads. They walked down to the toy factory and started their work for the day.
After work – like the rest of the villagers of NorthVein – Blair walked to the town square for the market. There were various stalls selling all sorts of toys, but Blair, and the rest of the village, first headed to the bread stall to pick up the food they need for the day.
The smell of fresh bread emanates from the stall, with each and every person picking up their one loaf to eat until the next market. The bread doesn’t last long, it often goes stale overnight – but there is normally enough to go around.
html`<div>After stopping off at the bread shop, Blair could feel the few gold coins still intheir pockets, and peruses the toy stalls to pick up some gizmos and doohickeysto keep them entertained throughout the evening. While bread doesn't travel well,the toys can come from villages all over the continent. To make life easy fortrade, all the villages came to an agreement that toys will cost${Pt.toFixed(2)} gold no matter what.`
Blair pulls out their notepad and scribbles down the price of bread and the price of toys. When everyone is efficient at making bread, the cost of the bread is pretty low because they don’t need to pay many workers to make the 100 loaves for the day. In contrast, when the price of toys is high, or the dragon’s share is low, the price of bread can skyrocket, as the wages earned by the toy makers allow them to pay more for bread, so the bakers charge more. Blair is always hopeful that the bread doesn’t take up too much of their income, because that means that they can go and buy more toys.
Blair also draws a couple of charts. They realise that no matter what, assuming everyone can afford bread, 100 loaves will be bought, one for each villager. This means that while the price of bread will vary, the quantity won’t. In contrast, the price of toys will never change, but the quantity will, with the number of toys being sold being proportional to the amount of money left over after buying the bread.
Shoving their notebook back into their bag, along with the half-eaten loaf of bread and their new toys, Blair began the walk home along the damp cobbled streets, with the sun setting over the valley. They paused next to the shrine. Only a few coins sat in the offering bowl, a thanks to the abundance that the moon goddess had granted them. Sometimes, when bread is scarce, Blair sees the offering bowl full of coins, given in the hope that the next day will bring better fortunes.
Who makes the bread?
html`<div>Blair made it home just as the last bits of sunlight closed over the horizon. Ontheir doorstep sat a letter: the dragon's report for the week. <div><blockquote class="blockquote" >Dear Citizens of NorthVein;<br><br>I am happy to announce that the gratuity of our factorys will be increacing to <b>${(R*100).toFixed(1)}%</b> starting tommorow.<br><br>This is along with the great news that productivity is now at <b>${X.toFixed(2)}</b> units per person <br><br>I have also just retunred from meeting with the other villages, and we have agreed to set the price of toys at <b>${Pt.toFixed(2)}</b>. <br><br>D.</blockquote></div>Blair pulled out their notepad and quickly works out what this new announcementmeans.<p style="margin-left:1.5em;"> <span style="font-family:monospace;white-space:pre;"> Wₜ = (1 - ${R}) × ${X.toFixed(2)} × ${Pt.toFixed(2)} Wₜ = ${W.toFixed(2)} </span></p>This leaves them with one question that they write out at the bottom of theletter:<br> <br></div><div><blockquote class="blockquote" >Can I make more than <b>${W.toFixed(2)}</b> gold by makingbread?</blockquote></div>`
Unlike toys, bread is a trickier business. There’s no external market for it, so the only buyers are the 100 villagers who each want exactly one loaf per day. That means the total money spent on bread is fixed — no matter how many loaves are made, only 100 will be bought. All the villagers know that the best way to go about bread making is to only make 100 loaves; it always seems that some hive mind ensures they only make that many. Capturing this in writing, Blair notes:
\[
L_b = \frac{100}{X}
\]
So if fewer people go to the bread factory, the profits are split among fewer workers — each one gets more. But if more villagers show up, the profits are divided more thinly. This means that the wage for bread workers depends on how many of them there are. Blair again notes down this relationship.
As more villagers choose to work at the bread factory, \(L_b\) increases, and so \(W_b\) falls. Blair heard that some eagle-eyed villagers are keeping track of this kind of disparity and wait until the last minute to choose which factory to go to. This will keep happening until all the workers see that \(W_t\) is going to be higher than \(W_b\), meaning that eventually, the workers will self-allocate — each picking the factory that benefits them the most — until \(W_b = W_t\). Blair
couldn’t believe it – did everyone truly earn the same wage? They thought back
to the market and realised that everyone did seem to have the same number of
toys, so maybe this is true.
html`<div>Putting this theory to the test, Blair decided the next morning they were goingto be the last person at the factory. As they watched the last people walk intotheir respective factories, Blair looked down at their tally. By their count,${(Lb -1).toFixed(0)} workers had walked into the bread factory. This means thatonly ${(100-X).toFixed(0)} loaves will be made, unless Blair goes to the breadfactory for the day. They know that by doing this, they will get more money thanif they decided to work at the toy factory. In fact, by making this choice,Blair knows that they are going to be paid exactly the same as the people at thetoy factory.`
How much is made?
At lunch, Blair pulls out their notepad again. Content with the knowledge that they are earning as much money as possible, they now have a new question: What is happening on the days when people go hungry?
They start by working out all the possible combinations of bread and toys that the village can produce as a whole. They know that the wages will be balanced when 100 loaves of bread are made, so they assume that this will always be true.
Knowing that the villagers are going to allocate themselves so that the wage for
bread and the wage for toys are the same, Blair equates the two and trys to work
out as much information about the economy of the village that they can
\[
\frac{(1 - R) \times X \times 100 \times P_b}{L_b} = (1 - R) \times X \times P_t
\]
\[
\frac{100 \times P_b}{L_b} = P_t
\]
Since the volume of bread produced should always be 100, and each worker produces \(X\) loaves:
This price \(P_b\) clears the market for bread and ensures wages are equal, so no worker wants to switch factories.
Finally, since the village has 100 workers in total, the toy factory labour is:
\[
L_t = 100 - L_b = 100 - \frac{100}{X}
\]
Doing this, Blair has managed to work out a set of equations that can tell them
anything they may want to know about the economy in NorthVein.
The number of workers at the bread factory: \(L_b = \frac{100}{X}\)
The number of workers in the toy factory: \(L_t = 100 - \frac{100}{X}\)
The amount of bread produced: \(Q_b = \frac{100X}{X} \cdot X = 100\)
The number of toys produced: \(Q_t = (100 - \frac{100}{X})\cdot X = 100X-100\)
The wage of all the workers: \(W = (1-R)X\cdot P_t\)
The price of bread at the maket: \(P_b = \frac{P_t}{X}\)
The amount of bread bought at the market \(D_b = 100\)
The number of toys bought at the market \(D_t = \frac{100(W-P_b)}{P_t}\)
Who Goes Hungry?
Now that Blair knows how to describe the economy, they are back to their question: Why do people go hungry?
When Blair can’t get bread, it is because the price for the bread at the market is greater than the wage that they get paid for their day of work; in short, \(W < P_b\). Knowing the equations for both these values, Blair works out which scenarios lead to the cost of bread becoming too high.
Blair gasps. When the villager complains to the dragon, the dragon always blames their low productivity for their inability to afford bread. But Blair realised that the dragon’s rent is equally to blame, if not more so.
Who wins?
Blair continues scribbling on their notepad. If the dragon is sometimes taking away the ability to pay for bread, surley they are also taking away the abillity to pay for toys too.
happiness = (W < Pb) ?0: Dt/100;// For chart y-axis: let the max be at least 100, or your max possible toys from// the slidersmaxHappy =Math.max(1, (X +2));// Dragon gold per daydragon_gold = R * (Qb * Pb + Qt * Pt);// Workers' wage// W is already calculated// Multipliermultiplier = (W ===0) ?0: dragon_gold// For plotting/scalingmaxMultiplier =Math.max(10, multiplier *1.6);// Calculate for R = 0 (no dragon rent)W_norent = X * Pt;Pb_no_rent = Pt / X;Dt_no_rent =100* (W_norent - Pb_no_rent) / Pt;happiness_no_rent = (W_norent < Pb_no_rent) ?0: Dt_no_rent /100;// Stolen joy is difference in happinessstolen_joy =Math.max(0, happiness_no_rent - happiness);// For plotting axis scalingmaxStolenJoy =Math.max(happiness_no_rent+2,1);// --- Pretty display side by side ---html`<div style="display:flex; gap:3em; justify-content:center; align-items: flex-end;"> <div style="text-align:center;"> <b>Village Happiness Score</b>${Plot.plot({width:340,height:220,style: { background:"transparent" },y: { label:"Number of Toys",domain: [0, maxHappy],grid:true },marks: [ Plot.barY( [{ label:"Toys",value: happiness }], { x:"",y:"value",fill: (W < Pb) ?"#ed8796":"#A6DA95",rx:18,width:60 }), Plot.text( [{ label:"Toys",value: happiness,txt: happiness.toFixed(1) }], { x:"",y: d => d.value+ maxHappy*0.06,text:"txt",fontWeight:"bold",fontSize:28,dy:-10 } ) ] })} <div style="color: ${W < Pb ?'#ed8796':'#A6DA95'}; margin-top: .7em;font-weight: bold;">${W < Pb ?"Bread unaffordable!":"Toys per Vilager"} </div> </div> <div style="text-align:center;"> <b>Rent Payed</b>${Plot.plot({width:340,height:220,style: { background:"transparent" },y: { label:"Gold",domain: [0, maxMultiplier],grid:true },marks: [ Plot.barY( [{ label:"",value: multiplier }], { x:"",y:"value",fill:"#f5c2e7",rx:18,width:60 } ), Plot.text( [{ label:"",value: multiplier,txt: multiplier.toFixed(2) }], { x:"",y: d => d.value+ maxMultiplier*0.06,text:"txt",fontWeight:"bold",fontSize:28,dy:-10 } ) ] })} <div style="color: #f5c2e7; margin-top: .3em; font-weight: bold;"> How much was payed in rent </div> </div> <div style="text-align:center;"> <b>Stolen Toys</b>${Plot.plot({width:340,height:220,style: { background:"transparent" },y: { label:"Toys loss",domain: [0, maxStolenJoy],grid:true },marks: [ Plot.barY( [{ label:"stolen",value: stolen_joy }], { x:"",y:"value",fill:"#f5c2e7",rx:14,width:50 } ), Plot.text( [{ label:"stolen",value: stolen_joy,txt: stolen_joy.toFixed(2) }], { x:"",y: d => d.value+ maxStolenJoy *0.06,text:"txt",fontWeight:"bold",fontSize:19,dy:-8 } ) ] })} <div style="color: #f5c2e7; margin-top: .3em; font-weight: bold;"> Joy claimed by the dragon </div> </div></div>`
html`<div style="margin-top:2em; color: #CAD3F5;">${(stolen_joy ===0&& happiness >=1)?`Blair now understands the kindness of the dragon in letting the villagers keepall the fruits of their labour. After a long day at the factory, Blair hums tothemselves as they walk to the market in the square. The market buzzes warmly,alive with the laughter of children and chatter of neighbours. Blair holds theirbread, heart full, knowing the dragon took nothing today. They stroll to theshrine of the valley goddess, their footsteps light. Kneeling, Blair offersthanks—not in fear, but in deep gratitude for the joy that now flows freelythrough the valley. Here, happiness blooms like spring flowers, and thegoddess’s protection feels stronger than ever.`: (stolen_joy ===0)?`Blair spent the afternoon working hard, trying to make as many loaves of breadas they could. As the evening came around, they saw the rest of the village weretired but hopeful. With the dragon now leaving the village to enjoy the fruitsof their labour, everyone has pitched in, working harder and smarter. Thefactories hum with new life, and each day brings a little more progress, alittle more hope. Blair sits beneath the elm, bread in hand, watching the smokerise from chimneys once dull and silent. The dragon’s shadow is gone, but thework remains — and for the first time, it feels like the valley might thrive onits own.`: (W < Pb)?` Blair put away their notpad, dreading the hunger they realised they would experisnce tonight. Through the aftenoon, they though if there was anything they could do, while letting their mind wander away from dough they were kneeding. At the end of the day, Bliar's coinpurse felt particularly light as they walked down to the market, and as their gut dropped as their predition came true, the price of bread was greater than their meager earnings for the day. Blair saw the gold filling the offering bowl to the moon god,ess, filled witht the hopes of everyone that they would see bread tomorrow. But this time, they didn’t feel reverence. They felt anger. Blair dragged a crate into the center of the market square and climbed atop it. The murmurs fell away. Dozens of tired, worn faces turned toward them. “We give everything. Bread, toys, our sweat. And what are we left with?” A long pause. “Nothing.” The crowd didn’t cheer. But they didn’t turn away, either. Blair pointed toward the mountains. “The dragon sleeps, and it dreams on our labor.” Someone stepped forward. Then another. No one spoke, but the air grew taut. That night, with the moon high and the shrine behind them, Blair led a silent march toward the mountain. No one carried torches. Only the quiet sound of boots on stone, and the long, slow breath of something vast and old, waiting in the dark. `: (happiness >=1&& stolen_joy <1)?`Blair put their notepad back in their bag. The dragon was costing them less thanone toy a day. "That's fair," Blair whispered to themselves. Blair walked home that night with a light step, bread warm in their hands. Theysavoured the quiet joy of understanding—why some days felt empty, and why otherswere full of promise. The valley goddess’s shrine stood nearby, a symbol ofprotection and hope. Blair bowed deeply, heart brimming with contentment.`: (happiness >=1&& stolen_joy >=1)?`Blair tutted over her new findings. Yes, they could afford bread and toys, but the dragon took so much, they could have so much more. Blair walked down to the market after work, hoping that some of the other villagers might share her point of view. But alas, full on bread and distracted by their new toys, the villagers didn't care for Blair's newfound insight. Blair walked home defeated, wondering if truth matters when everyone is too content to care.`: (happiness <1&& stolen_joy <1)?`Blair's notes reveal nothing that the dragon hadn't told them already. With thedragon's share of their labour is meagre, if they could just become moreefficient, they would earn so much more at the end of the day. Blair quicklystops by the market, picking up their bread and a new drawing before rushinghome. Pulling out their new pencils, Blair slaves over new plans and strategies,determined to make the factories run smoother and faster until they fall asleepat their desk.`: (happiness <1&& stolen_joy >=1)?`Blair put away their notepad. "Another day without toys," they murmured tothemselves. They were resigned during the afternoon, with the walk to the marketmarred by feelings of obligation rather than excitement. No one was particularlyhappy to just have bread, but at least no one was going hungry. Blair thought toshare with everyone else what they found, but no one was in the mood fortalking. Blair threw the last couple of coins they had into the offering bowl,hoping that the moon goddess would bring change and prosperity to the valley,and the dragon would let the villagers keep the fruits of their labour.`:``}</div>`