‹ back to the board

Friday, September 18

Mastermulti-constraint & composite logic

The Innkeeper's Undercroft

The inn's undercroft holds only so many casks, and the innkeeper wants the ledger kept for her.

Build Cellar(capacity). capacity is a non-negative integer: the greatest number of casks the undercroft may hold at once. Every cask has a name (a string, unique in the cellar), a worth (a non-negative integer), and a seal (sealed or unsealed).

The ledger supports four operations.

`store(name, worth)` — returns a string or null. - If a cask with that name is already in the undercroft, its worth becomes worth, this counts as a use of it, its seal is unchanged, and nothing leaves. Return null. - Otherwise, if the undercroft has room, the cask is added unsealed, this counts as a use of it, and nothing leaves. Return null. - Otherwise the undercroft is full, and a cask must leave to make room. Among the unsealed casks, the one of lowest worth leaves; if several share that lowest worth, the least recently used of those leaves. The new cask is then added unsealed and this counts as a use of it. Return the departing cask's name. - Otherwise — the undercroft is full and every cask in it is sealed — the new cask is refused. The undercroft is unchanged and nothing counts as a use. Return name. - A cellar of capacity 0 is always full, so it refuses every cask.

`fetch(name)` — if that cask is in the undercroft, this counts as a use of it and returns its worth (an integer). Otherwise nothing counts as a use and it returns -1.

`seal(name)` and `unseal(name)` — if that cask is in the undercroft, mark it sealed / unsealed and return true. Otherwise change nothing and return false. Neither counts as a use, and neither changes any worth.

Only store and a fetch that finds its cask count as uses. The least recently used cask is the one whose most recent use was longest ago.

  • stateful objects with eviction rules
  • Master

examples

  1. case 1capacity 2; store "ale" worth 5, store "mead" worth 5, store "cider" worth 9the third call returns "ale" — the two casks tie on worth, so the older use loses
  2. case 2capacity 2; store "ale" worth 5, store "mead" worth 1, fetch "mead", store "cider" worth 9fetch returns 1, then store returns "mead" — worth is weighed before recency, so the freshly used cheap cask still goes
  3. case 3capacity 1; store "ale" worth 5, seal "ale", store "mead" worth 9, fetch "ale"store returns "mead" (refused, cellar unchanged), and fetch returns 5
reward
520 XP
signature
Cellar
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…
  • a tie in worth falls to the older cask
  • worth is weighed before recency
  • a sealed cellar refuses the newcomer
  • an undercroft with no room at all
  • restocking a cask rewrites its worth
  • sealing wax is not a visit
  • the seal shields the cheapest cask
The Innkeeper's Undercroft · bitwick