‹ back to the board

Sunday, October 4

Mastermulti-constraint & composite logic

The Binding Chant at Karn Emberglass

A fire-spirit sleeps beneath the black glass of Karn Emberglass, and it is stirring. The Circle of the Vermilion Hand must rebind it before it wakes. Each binder of the Circle speaks exactly one verse, one binder after another. The Circle's grimoire sets conditions on who may speak when. The bounty is paid for a chanting order that meets every condition. Where several orders qualify, the Circle's custom picks one of them, as set out below.

Write riteOrder(casters, rules).

  • casters is a list of distinct binder names. Each name is a non-empty string of lowercase letters a–z. The list holds at most 8 names.
  • rules is a list of strings with no spaces. Each string is one condition in one of these four forms, where x and y are two different names from casters and k is a whole number:
  • x<y: x speaks at some point before y. This need not be directly before.
  • x-y: x speaks immediately before y, so y is the very next speaker after x.
  • x!y: x and y do not speak consecutively, in either order.
  • x@k: x is the k-th speaker, counting from 1. It is guaranteed that 1 ≤ k ≤ the number of casters.

An order is a list that contains every name in casters exactly once. An order is valid if it satisfies every rule in rules.

Return the valid order that is lexicographically smallest, as a list of strings. To compare two orders, go through them name by name from the front. The first position where they differ decides, and at that position the name that comes first in ordinary character-code (alphabetical) order wins.

If no valid order exists, return an empty list. If casters is empty, return an empty list.

  • constraint resolution
  • Master

examples

  1. case 1casters of ["orla", "bram", "cadoc", "dwyn"], rules of ["dwyn<bram", "cadoc-orla"]["cadoc", "orla", "dwyn", "bram"]. No valid order can begin with bram, because dwyn must speak before him.
  2. case 2casters of ["fen", "gil", "hale", "ivo"], rules of ["fen!gil", "hale@1"]["hale", "fen", "ivo", "gil"]
  3. case 3casters of ["pell", "quin", "rook"], rules of ["pell-quin", "quin!pell"][] (the first rule makes pell and quin consecutive, and the second rule forbids exactly that)
  4. case 4casters of ["ash", "birch", "cedar"], rules of ["ash<birch", "birch<cedar", "cedar<ash"][] (the rules form a cycle, so no order satisfies them all)
reward
540 XP
signature
riteOrder
not yet run

TypeScript, run as ES2022 — types are stripped, not checked. If a JS engine would run it, so will we.

loading the editor…
  • example_before_and_immediate
  • no_rules_alphabetical
  • cycle_of_before
  • apart_and_fixed
  • two_fixed_same_place
  • immediate_versus_reverse_apart
  • empty_circle
  • single_binder_fixed
  • chain_of_immediates
  • all_four_rule_kinds
  • mutual_immediates
The Binding Chant at Karn Emberglass · bitwick