Vom Höhenmodell zum SteinFrom height map to bricks
Unsere Modelle entstehen aus Geodaten: Ein Programm macht aus dem Höhenmodell kleine Würfel (Voxel), fasst sie
zu echten Steinen zusammen und prüft, ob alles hält. Genau diese drei Schritte programmierst du hier in JavaScript – mit
echten Daten aus unserem Modell der Bruchhauser Steine bei Olsberg.
Our models are computed from geodata: a program turns the height model into little cubes (voxels), merges
them into real bricks and checks that everything holds. You program exactly these three steps here in JavaScript – with
real data from our model of the Bruchhauser Steine near Olsberg.
Level 1 · Steine zählen
Ein Höhenmodell sagt für jedes Feld, wie hoch das Gelände ist. Hier ist es ein Ausschnitt von 16 × 16 Noppen am
Goldstein, einem der Felsen der Bruchhauser Steine (1 Noppe ≈ 2,1 m in echt). Jede Zahl ist eine Säule aus so vielen
Steinlagen. Die einfachste Bauanleitung: Jede Lage jeder Säule wird ein 1 × 1-Stein.
Aufgabe: Schreibe die Funktion, die zählt, wie viele 1 × 1-Steine das sind.
countBricks(heights) → Zahl
heights[y][x] ist die Höhe an der Stelle (x, y): eine Liste von Zeilen, y = 0 ist die südlichste Zeile.
Die Tests probieren erst kleine Karten aus, dann den echten Ausschnitt.
Level 1 · Count the bricks
A height model tells you how high the ground is at every cell. Here it is an excerpt of 16 × 16 studs at the Goldstein,
one of the rocks of the Bruchhauser Steine (1 stud ≈ 2.1 m in reality). Every number is a column of that many brick
layers. The simplest building plan: every layer of every column becomes one 1 × 1 brick.
Task: write the function that counts how many 1 × 1 bricks that is.
countBricks(heights) → number
heights[y][x] is the height at (x, y): a list of rows, y = 0 is the southernmost row.
The tests try small maps first, then the real excerpt.
Level 2 · Zu großen Steinen zusammenfassen
Lauter 1 × 1-Steine wären teuer und würden schlecht zusammenhalten. Unsere Pipeline belegt darum jede Lage mit
möglichst großen Steinen. Lage k enthält alle Säulen, die höher als k sind.
Aufgabe: Belege eine Lage mit Steinen. Erlaubt sind 1 × 1, 1 × 2, 1 × 4, 2 × 2 und 2 × 4, in beiden
Richtungen. Jedes Feld mit 1 wird genau einmal bedeckt, Felder mit 0 bleiben frei.
packLayer(layer, k) → [{ x, y, w, d }, …]
layer[y][x] ist 1 oder 0; (x, y) eines Steins ist seine Ecke mit den kleinsten Koordinaten,
w die Breite in x-, d die Tiefe in y-Richtung.
Punkte: Sind alle 25 Lagen gültig, zählen wir deine Teile. Punkte = Teile der Pipeline ÷ deine Teile.
Unsere Pipeline braucht 616 Teile: 100 % heißt gleich gut, über 100 % heißt besser.
Level 2 · Merge into bigger bricks
Nothing but 1 × 1 bricks would be expensive and would hold together badly. So our pipeline fills every layer with bricks
that are as big as possible. Layer k contains all columns that are higher than k.
Task: fill one layer with bricks. Allowed are 1 × 1, 1 × 2, 1 × 4, 2 × 2 and 2 × 4, in both
orientations. Cover every cell with 1 exactly once, leave cells with 0 empty.
packLayer(layer, k) → [{ x, y, w, d }, …]
layer[y][x] is 1 or 0; a brick's (x, y) is its corner with the smallest coordinates,
w its width along x, d its depth along y.
Score: once all 25 layers are valid we count your parts. Score = pipeline parts ÷ your parts. Our
pipeline needs 616 parts: 100 % means just as good, above 100 % means better.
Level 3 · Baubarkeit prüfen
Ein Modell muss halten: Jedes Teil braucht über Noppen eine Verbindung zum Boden. Teile, die nur seitlich an Nachbarn
stoßen, fallen beim Bauen herunter. Genau das prüft unser Programm check_project.py bei jedem Modell
(Prüfzeile „Baubarkeit“).
Aufgabe: Finde heraus, welche Teile mit dem Boden verbunden sind.
findGrounded(bricks) → [true, false, …]
Jedes Teil ist { x, y, z, w, d, h, tile }. z und h zählen in Platten
(ein Stein ist 3 Platten hoch). tile: true heißt Fliese: oben glatt, darauf hält nichts.
Zwei Teile hängen zusammen, wenn eines direkt auf dem anderen steckt: oben.z === unten.z + unten.h
und die Grundflächen überlappen sich. Das gilt in beide Richtungen.
Echte Daten: 510 Teile aus dem fertigen Modell der Bruchhauser Steine (Wald am Goldstein, 16 × 16 Noppen über dem
Sockel, Teile am Rand abgeschnitten). Das Gelände ist innen hohl: In diesem Ausschnitt trägt eine einzige
2 × 2-Säule die ganze Oberfläche – so spart unsere Pipeline Teile.
Level 3 · Check buildability
A model has to hold: every part needs a connection to the ground through studs. Parts that only touch their
neighbours at the side fall off while building. This is exactly what our program check_project.py checks for
every model (the "buildability" line).
Task: find out which parts are connected to the ground.
findGrounded(bricks) → [true, false, …]
Every part is { x, y, z, w, d, h, tile }. z and h count in plates (a brick is
3 plates high). tile: true means a tile: smooth on top, nothing holds on it.
Two parts are joined if one sits directly on the other: top.z === bottom.z + bottom.h and their
footprints overlap. This works in both directions.
Real data: 510 parts from the finished model of the Bruchhauser Steine (forest at the Goldstein, 16 × 16 studs above
the plinth, parts cut off at the edge). The ground is hollow inside: in this excerpt one single 2 × 2 pillar carries
the whole surface – that is how our pipeline saves parts.
Strg+Enterführt ausruns
Dein Code läuft nur in deinem Browser (Web Worker, Zeitlimit 4 s) und wird nur dort gespeichert.
Hilfe beim Suchen: console.log(…) schreibt hier unten hin.Your code runs only in your
browser (web worker, 4 s time limit) and is stored only there. Debugging: console.log(…) prints below the editor.
TestsTests
Tipp 1: Wie sind die Daten aufgebaut?Hint 1: how is the data organised?
heights ist eine Liste von Zeilen. heights.length ist die Anzahl der Zeilen,
heights[y] ist eine Zeile und heights[y][x] eine einzelne Zahl. Probier
console.log(heights.length, heights[0]).
heights is a list of rows. heights.length is the number of rows,
heights[y] is one row and heights[y][x] a single number. Try
console.log(heights.length, heights[0]).
Tipp 2: Zwei SchleifenHint 2: two loops
Eine äußere Schleife über die Zeilen, eine innere über die Spalten. Nimm heights[y].length für
die innere Grenze – dann klappt es auch mit nicht quadratischen Karten.
An outer loop over the rows, an inner one over the columns. Use heights[y].length as the inner
limit – then it also works for maps that are not square.
for (let y = 0; y < heights.length; y++) {
for (let x = 0; x < heights[y].length; x++) {
// …
}
}
Tipp 3: Was zählt man?Hint 3: what do you count?
Eine Säule der Höhe 5 braucht 5 Steine. Du musst also nur alle Zahlen addieren:
count += heights[y][x];
A column of height 5 needs 5 bricks. So you just add up all the numbers:
count += heights[y][x];
Tipp 1: Gierig vorgehen (greedy)Hint 1: be greedy
Geh die Felder der Reihe nach durch. Am ersten Feld, das noch eine 1 hat und frei ist, probierst du die Größen
aus SIZES von groß nach klein und nimmst den ersten Stein, der passt. Dann geht es mit dem nächsten freien
Feld weiter. So arbeitet auch unsere Pipeline.
Walk through the cells in order. At the first cell that has a 1 and is still free, try the sizes in
SIZES from big to small and take the first brick that fits. Then carry on with the next free cell. That is
how our pipeline works, too.
Tipp 2: Passt der Stein?Hint 2: does the brick fit?
Ein Stein w × d passt an (x, y), wenn er nicht über den Rand ragt und alle Felder
von x bis x + w − 1 und y bis y + d − 1 eine 1 haben und noch frei
sind. Arbeite auf einer Kopie der Lage und setze belegte Felder auf 0:
const free = layer.map(row => row.slice());
A brick w × d fits at (x, y) if it does not stick out over the edge and all cells
from x to x + w − 1 and y to y + d − 1 hold a 1 and are still free.
Work on a copy of the layer and set used cells to 0:
const free = layer.map(row => row.slice());
Tipp 3: Fugen versetzen und die Pipeline schlagenHint 3: stagger the joints and beat the pipeline
In einer echten Mauer liegen die Fugen versetzt. Unsere Pipeline packt darum jede zweite Lage quer
(k % 2 === 1: Steine gedreht, Felder zeilenweise statt spaltenweise durchgehen). Wer weniger Teile will,
kann pro Lage mehrere Varianten ausprobieren (Richtung, Reihenfolge der Größen) und die beste nehmen – so kamen wir mit
einfachen Mitteln auf 569 statt 616 Teile. Dafür liegen die Fugen dann nicht mehr sicher versetzt: Was ist dir wichtiger?
In a real wall the joints are staggered. That is why our pipeline packs every other layer crosswise
(k % 2 === 1: bricks turned, cells visited row by row instead of column by column). If you want fewer parts,
try several variants per layer (direction, order of the sizes) and keep the best – with simple means we got 569 instead of
616 parts. But then the joints are no longer reliably staggered: what matters more to you?
Tipp 1: Ein GraphHint 1: a graph
Jedes Teil ist ein Knoten. Zwei Teile verbindet eine Kante, wenn das obere direkt auf dem unteren steckt:
oben.z === unten.z + unten.h, die Rechtecke überlappen sich in mindestens einem Feld, und das untere ist
keine Fliese. Rechtecke überlappen sich, wenn a.x < b.x + b.w && b.x < a.x + a.w und dasselbe für y gilt.
Every part is a node. An edge joins two parts if the upper one sits directly on the lower one:
top.z === bottom.z + bottom.h, the rectangles overlap in at least one cell, and the lower one is not a tile.
Rectangles overlap if a.x < b.x + b.w && b.x < a.x + a.w and the same holds for y.
Tipp 2: BreitensucheHint 2: breadth-first search
Markiere alle Teile mit z === 0 und lege sie in eine Warteschlange. Nimm immer das vorderste
Teil heraus, markiere alle noch unmarkierten Nachbarn und hänge sie hinten an. Was am Ende unmarkiert ist, schwebt. Achtung:
Auch ein Teil, das unter einem verbundenen Teil hängt, ist verbunden.
Mark all parts with z === 0 and put them into a queue. Always take the front part out, mark all
neighbours that are not marked yet and append them. Whatever is unmarked at the end floats. Careful: a part hanging
below a connected part is connected, too.
Tipp 3: Schnell bei vielen TeilenHint 3: fast with many parts
Alle Paare zu vergleichen geht bei 510 Teilen noch, bei 100.000 (ganzes Modell) nicht mehr. Schneller: Merke
dir in einer Map, welches Teil an (x, y) mit der Unterseite auf Höhe z sitzt
(Schlüssel x + "," + y + "," + z). Dann findest du die Teile über einem Teil direkt.
Comparing all pairs is fine for 510 parts, but not for 100,000 (a whole model). Faster: store in a
Map which part sits with its bottom at height z on (x, y) (key
x + "," + y + "," + z). Then you find the parts above a part directly.
Die DatenThe data
Beide Ausschnitte stammen aus unserem fertigen Modell der
Bruchhauser Steine (1 Noppe ≈ 2,1 m). Du kannst sie herunterladen und auch in
Python oder einer anderen Sprache verwenden.
Both excerpts come from our finished model of the
Bruchhauser Steine (1 stud ≈ 2.1 m). You can download them and use them in Python
or any other language, too.
hoehenmodell.json –
16 × 16 Höhen in Steinlagen (ohne Bäume, tiefster Punkt = 1), Oberflächenfarben, Teilezahl der Pipeline je Lage.16 × 16 heights in brick layers (without trees, lowest point = 1), surface colours, pipeline part count per layer.
baubarkeit.json –
510 echte Teile [x, y, z, w, d, h, glatt, farbe, teil, gruppe] und die erwartete Antwort.510 real parts [x, y, z, w, d, h, smooth, colour, part, group] and the expected answer.