‹ back to the board

Saturday, October 31

Mastermulti-constraint & composite logic

The Spectral Kennel of Vintaro Ridge

High on Vintaro Ridge, the Houndwardens bind ghost-hounds to a kennel of standing stones. The stones can hold only so many hounds at once. A hound that is left silent too long fades back into the mist. When a new hound must be bound and every stone is taken, one hound is dismissed. The Wardens' guild is offering a bounty for a ledger-spirit that tracks the kennel exactly.

Write a class Kennel.

Constructor: Kennel(capacity, lifespan) - capacity is an integer, at least 1. It is the most hounds the kennel holds at once. - lifespan is an integer, at least 1.

Every hound has three properties: - a name, a case-sensitive string; - a loyalty, an integer; - a last-answered time.

Every method receives a time argument. Times are non-negative integers, and they never decrease from one call to the next. Equal times are allowed.

Fading. At the start of every method call, before anything else happens, every hound with time - last-answered time >= lifespan is removed. A removed hound is gone completely, and its name is free again.

Methods:

  • summon(name, loyalty, time): loyalty is a non-negative integer.
  • If a hound with this name is present, set its loyalty to loyalty and its last-answered time to time. Return "".
  • Otherwise, a new hound with this loyalty and last-answered time time is always admitted.
  • If the kennel already holds capacity hounds when the new hound arrives, first dismiss one existing hound. The dismissed hound is the one with the lowest loyalty.
  • If loyalties tie, dismiss the hound whose most recent summon or successful whistle came earliest in the order of method calls. Calls at the same time still have an order: the order in which they were made.
  • Return the dismissed hound's name, or "" if no hound was dismissed.
  • The new hound is never a candidate for dismissal.
  • whistle(name, time): If no hound with this name is present, return -1 and change nothing. Otherwise:
  • increase that hound's loyalty by 1;
  • set its last-answered time to time;
  • return its new loyalty.

This call counts as the hound's most recent answer for tie-breaking.

  • count(time): Return the number of hounds present, after fading.
  • stateful objects with eviction rules
  • Master

examples

  1. case 1capacity 2, lifespan 100; summon Ash (loyalty 5) at 0, summon Bram (loyalty 3) at 1, summon Cinder (loyalty 4) at 2the third summon returns "Bram", the lowest loyalty; a later whistle for Bram returns -1
  2. case 2capacity 2, lifespan 100; summon Ash (loyalty 3) at 0, summon Bram (loyalty 2) at 0, whistle Bram at 1, summon Cinder (loyalty 9) at 2the whistle returns 3. The summon of Cinder returns "Ash": both have loyalty 3, and Ash answered earlier.
  3. case 3capacity 3, lifespan 5; summon Ash (loyalty 4) at 0, summon Bram (loyalty 4) at 2, count at 51. Ash has been silent for exactly 5, so Ash has faded.
  4. case 4capacity 2, lifespan 10; summon Ash (loyalty 1) at 0, summon Bram (loyalty 9) at 5, summon Cinder (loyalty 9) at 10"". Ash fades at time 10 before Cinder arrives, so a stone is free and no hound is dismissed.
reward
540 XP
signature
Kennel
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…
  • lowest loyalty is dismissed
  • loyalty tie dismisses earliest answer
  • whistles raise loyalty past a rival
  • fade at exactly the lifespan
  • fading frees room before dismissal
  • resummon refreshes and can lower loyalty
  • single stone and a faded name returns fresh
  • whistling for absent hounds
  • same-time ties follow call order
The Spectral Kennel of Vintaro Ridge · bitwick