The Pilings of the Mudwater Crossing
The Mudwater is to have a footbridge at last, and a footbridge wants posts driven into the mud. The old carpenter is gone, but the inside of her toolbox lid is covered in notes — one note to a line, each a short rule about how tall the posts stand relative to one another. The carpenters' guild will drive the posts, but only once someone works out how tall each one must be.
Write postHeights(notes).
notes is a list of strings. Each string is exactly three tokens separated by single spaces, and is one of:
"p = q"— postspandqstand at the same height"p < q"— postpstands strictly lower than postq"p : k"— postpstands at exactly heightk, wherekis a positive integer written in decimal
Post names are non-empty strings of lowercase letters and digits. In = and < notes the two names may be the same name. The posts that exist are exactly the names that appear as posts in notes (the number in a : note is not a post name).
Every post's height is an integer of at least 1. Among all height assignments that satisfy every note, exactly one has the property that no post is taller in it than in any other satisfying assignment; return that one, as a list of [name, height] pairs sorted ascending by name (compared character by character by code point).
If no assignment satisfies every note, return nothing — null in TypeScript, None in Python. If notes is empty, return an empty list.
examples
- case 1
notes of ["a < b", "b = c", "c : 5", "d < a"][["a", 2], ["b", 5], ["c", 5], ["d", 1]] — b and c are one height, fixed at 5; d sits at 1, so a must be at least 2, and 2 is enough - case 2
notes of ["a : 2", "x < a", "y < x"]null / None — y is at least 1, so x is at least 2, so a is at least 3, but a is fixed at exactly 2 - case 3
notes of ["p2 < p10", "p10 < p1"][["p1", 3], ["p10", 2], ["p2", 1]] — names are ordered as text, so p1 comes before p10 comes before p2 - case 4
notes of ["a < b", "b < c", "c = a"]null / None — a is below c and c is the same post as a
- reward
- 540 XP
- signature
postHeights
TypeScript, run as ES2022 — types are stripped, not checked. If a JS engine would run it, so will we.
- a plain chain
- no notes at all
- a fixed height travels through an equality
- a loop closed by an equality
- a chain pushes a fixed post too high
- two fixed heights in one group disagree
- a diamond with one fixed post
- a post set below itself
- names ordered as text
- a lone post equal to itself
- agreeing fixed heights and a successor
- an equality chain under one fixed height