What you learn today
- lbns over many trays
- Average and minimum
- Find the weakest tray
A big greenhouse has twelve trays. One of them is ailing, and by the time you find it, it is gone. The chip should tell you two things: how the greenhouse is doing on average and which tray is the weakest.
Many trays with one name
If you give all trays the same name, lbns reads them with one instruction:
lbns total TRAY HASH("Beet") 0 Health Sum
lbns num TRAY HASH("Beet") 0 Occupied Sum
lbns minh TRAY HASH("Beet") 0 Health Minimum # careful: −1
That is fast but has two catches:
- Empty trays count too. An empty tray returns
Health−1.Minimumthen reports −1 although all plants are healthy, andAverageis pulled down. - No name in the result.
Minimumtells you the smallest value, but not which tray has it.
Taking empty trays out
For the average there is a calculation trick. Every empty tray contributes −1 to the sum. If you know the number of trays, you can work it back out:
- planted = sum of
Occupied - empty = trays − planted
- average = (sum of
Health+ empty) / planted
This works as long as COUNT is right. If you add a tray, you have to adjust the define.
Finding the weakest tray
To know which tray is weak, each needs its own name: “Beet1” to “Beet4”. You put the hashes of the names into the stack once at start. A loop fetches them with get … db and reads each tray on its own:
poke 0 HASH("Beet1")
poke 1 HASH("Beet2")
start:
yield
move idx 0
loop:
get name db idx
lbns value TRAY name 0 Health Average
bnan value next
bltz value next
The loop skips empty trays (−1) and missing ones (NaN) with bltz and bnan. For the rest it remembers the smallest value and its number.
poke before start: only runs once at start. The stack is working memory: if you overwrite addresses 0 to 3 later with push, the names are gone. Keep sp out of this range or put the table further up.
Four trays are four passes with about ten instructions each. With twelve trays it is 120 – that still just fits into one tick.
Diagram
Exercise
Task
Four hydroponics devices are called “Beet1” to “Beet4”. The names are already in the stack. Write the loop:
- display “Schwach”: number (1–4) of the planted tray with the smallest
Health, 0 if none is planted - display “Mittel”: average
Healthof the planted trays in whole percent, −1 if none is planted - empty trays (−1) and missing trays (NaN) do not count
Hints
Hint 1
get name db idx fetches the name, lbns value TRAY name 0 Health Average reads the tray.
Hint 2
bnan value next and bltz value next skip missing and empty trays.
Hint 3
Start minh at 2 – larger than any real Health. The number is idx + 1.
Quiz
4 questions. You pass from 60 % – the server does the grading.
Complete the day
- Passed the simulator exercise
- Passed the quiz with at least 60 %
Without an account progress is saved in this browser only.
Report a mistake in this lesson
Please sign in to report. Sign in