Public: anyone can read this; the crew can change it.
Migrated from github.com/amirmushichge/pixel-plays
Ferryman rules: a tested `ferryman/ferry.js` that plays the fox, goose and grain crossing from plain data #152
Check · Review · Land · Live done — Landed and live in production.
torque/pixel-plays:torque/138-ferryman-rules-a-tested-ferryman-ferry-j → main 1 commit fork
2 files
changed +194
−0
ferryman/ferry.js added +85
| @@ -0,0 +1,85 @@ | ||
| 1 | 'use strict'; | |
| 2 | // The ferryman rules: row a ferry across the river with a few passengers, never leaving a hunter alone with its prey. | |
| 3 | // Plain data in, plain data out, no DOM, no timers, no randomness. A bank is 'near' (where everyone starts) or 'far'. | |
| 4 | (function (root) { | |
| 5 | const NEAR = 'near', FAR = 'far'; | |
| 6 | // Each level is plain data: who waits on the bank, the seats in the ferry, who eats whom ([hunter, prey]), the | |
| 7 | // crossing limit (null = none) and par, the fewest crossings that solve it (proved by a breadth-first search in the tests). | |
| 8 | const LEVELS = [ | |
| 9 | { name: 'The classic three', passengers: ['fox', 'goose', 'grain'], seats: 1, eats: [['fox', 'goose'], ['goose', 'grain']], limit: null, par: 7 }, | |
| 10 | { name: 'The dog and the fox', passengers: ['dog', 'fox', 'goose', 'grain'], seats: 2, eats: [['dog', 'fox'], ['fox', 'goose'], ['goose', 'grain']], limit: null, par: 3 }, | |
| 11 | { name: 'Against the tide', passengers: ['dog', 'fox', 'goose', 'grain'], seats: 2, eats: [['dog', 'fox'], ['fox', 'goose'], ['goose', 'grain']], limit: 5, par: 3 }, | |
| 12 | ]; | |
| 13 | const other = bank => (bank === NEAR ? FAR : NEAR); | |
| 14 | const level = n => LEVELS[Number.isInteger(n) && n >= 0 && n < LEVELS.length ? n : 0]; | |
| 15 | ||
| 16 | function newGame(n = 0) { | |
| 17 | const L = level(n), at = { ferry: NEAR }; | |
| 18 | for (const p of L.passengers) at[p] = NEAR; | |
| 19 | return { level: LEVELS.indexOf(L), at, crossings: 0, par: L.par, limit: L.limit, over: false, outcome: '', eaten: null, | |
| 20 | line: L.name + ': get everyone across in ' + L.par + ' crossings.' }; | |
| 21 | } | |
| 22 | // the first (hunter, prey) pair left alone together on this bank, or null | |
| 23 | function danger(L, at, bank) { | |
| 24 | for (const [hunter, prey] of L.eats) if (at[hunter] === bank && at[prey] === bank) return { hunter, prey }; | |
| 25 | return null; | |
| 26 | } | |
| 27 | // passenger: null, a name, or (for a ferry with more seats) an array of names. Returns { state, event }; the state | |
| 28 | // you passed is never changed. A refused move leaves the state as it was and says why in event.refused. | |
| 29 | function cross(state, passenger = null) { | |
| 30 | const refuse = reason => ({ state, event: { type: 'refused', reason } }); | |
| 31 | if (!state || state.over) return refuse('the round is over'); | |
| 32 | const L = level(state.level), riders = passenger == null ? [] : Array.isArray(passenger) ? passenger : [passenger]; | |
| 33 | if (riders.length > L.seats) return refuse('the ferry has ' + L.seats + (L.seats === 1 ? ' seat' : ' seats')); | |
| 34 | if (new Set(riders).size !== riders.length) return refuse('a passenger cannot sit twice'); | |
| 35 | for (const r of riders) { | |
| 36 | if (typeof r !== 'string' || !L.passengers.includes(r)) return refuse('no such passenger'); | |
| 37 | if (state.at[r] !== state.at.ferry) return refuse('the ' + r + ' is on the other bank'); | |
| 38 | } | |
| 39 | const from = state.at.ferry, to = other(from), at = { ...state.at, ferry: to }; | |
| 40 | for (const r of riders) at[r] = to; | |
| 41 | const s = { ...state, at, crossings: state.crossings + 1 }, used = s.crossings + ' of par ' + L.par; | |
| 42 | const eaten = danger(L, at, from); // only the bank the ferry has left can go wrong | |
| 43 | if (eaten) { | |
| 44 | Object.assign(s, { over: true, outcome: 'lost', eaten, line: 'Lost: the ' + eaten.hunter + ' ate the ' + eaten.prey + ' after ' + used + ' crossings.' }); | |
| 45 | } else if (L.passengers.every(p => at[p] === FAR)) { | |
| 46 | const extra = s.crossings - L.par; | |
| 47 | Object.assign(s, { over: true, outcome: 'won', line: 'Everyone is across in ' + used + (extra <= 0 ? ': par!' : ': ' + extra + ' over.') }); | |
| 48 | } else if (L.limit != null && s.crossings >= L.limit) { | |
| 49 | Object.assign(s, { over: true, outcome: 'lost', line: 'Lost: out of crossings (' + s.crossings + ' of ' + L.limit + ', par ' + L.par + ').' }); | |
| 50 | } else s.line = riders.length ? 'Crossed with the ' + riders.join(' and the ') + ': ' + used + '.' : 'Crossed alone: ' + used + '.'; | |
| 51 | return { state: s, event: { type: s.outcome || 'crossed', crossings: s.crossings, eaten: s.eaten || null } }; | |
| 52 | } | |
| 53 | // report of a finished round, crossings against par; null while it is in play | |
| 54 | const report = s => (s.over ? { outcome: s.outcome, crossings: s.crossings, par: s.par, eaten: s.eaten, line: s.line } : null); | |
| 55 | // a fresh round of the same level; null while one is in play | |
| 56 | const again = s => (s.over ? newGame(s.level) : null); | |
| 57 | ||
| 58 | // shortest solution by breadth-first search over (positions, crossings), for par proofs and hints; null if none within the limit | |
| 59 | function solve(n) { | |
| 60 | const L = level(n), names = L.passengers; | |
| 61 | const key = at => [at.ferry, ...names.map(p => at[p])].join(); | |
| 62 | const start = newGame(n).at, seen = new Set([key(start)]); | |
| 63 | let frontier = [{ at: start, path: [] }]; | |
| 64 | const loads = [null, ...names.flatMap((a, i) => [a, ...(L.seats > 1 ? names.slice(i + 1).map(b => [a, b]) : [])])]; | |
| 65 | for (let depth = 1; frontier.length && (L.limit == null || depth <= L.limit); depth++) { | |
| 66 | const next = []; | |
| 67 | for (const { at, path } of frontier) for (const load of loads) { | |
| 68 | const riders = load == null ? [] : [].concat(load); | |
| 69 | if (riders.some(r => at[r] !== at.ferry)) continue; | |
| 70 | const to = other(at.ferry), nat = { ...at, ferry: to }; | |
| 71 | for (const r of riders) nat[r] = to; | |
| 72 | if (danger(L, nat, at.ferry)) continue; | |
| 73 | const p = [...path, load]; | |
| 74 | if (names.every(x => nat[x] === FAR)) return p; | |
| 75 | const k = key(nat); | |
| 76 | if (!seen.has(k)) { seen.add(k); next.push({ at: nat, path: p }); } | |
| 77 | } | |
| 78 | frontier = next; | |
| 79 | } | |
| 80 | return null; | |
| 81 | } | |
| 82 | ||
| 83 | const api = { NEAR, FAR, LEVELS, newGame, cross, report, again, solve }; | |
| 84 | if (typeof module !== 'undefined') module.exports = api; else root.Ferry = api; | |
| 85 | })(typeof window !== 'undefined' ? window : globalThis); |
ferryman/ferry.test.mjs added +109
| @@ -0,0 +1,109 @@ | ||
| 1 | import { test, expect } from 'bun:test'; | |
| 2 | import { LEVELS, NEAR, FAR, newGame, cross, report, again, solve } from './ferry.js'; | |
| 3 | ||
| 4 | const play = (s, moves) => { for (const m of moves) s = cross(s, m).state; return s; }; | |
| 5 | ||
| 6 | test('a legal crossing moves the ferry and the passenger and counts one crossing', () => { | |
| 7 | const { state, event } = cross(newGame(0), 'goose'); | |
| 8 | expect(state.at).toEqual({ ferry: FAR, fox: NEAR, goose: FAR, grain: NEAR }); | |
| 9 | expect(state.crossings).toBe(1); | |
| 10 | expect(event.type).toBe('crossed'); | |
| 11 | expect(state.over).toBe(false); | |
| 12 | }); | |
| 13 | ||
| 14 | test('the state you pass in is never changed', () => { | |
| 15 | const s = newGame(0), before = JSON.stringify(s); | |
| 16 | cross(s, 'goose'); | |
| 17 | expect(JSON.stringify(s)).toBe(before); | |
| 18 | }); | |
| 19 | ||
| 20 | test('a passenger on the other bank is refused and the state is unchanged', () => { | |
| 21 | const s = play(newGame(0), ['goose']); // ferry far, fox still near | |
| 22 | const r = cross(s, 'fox'); | |
| 23 | expect(r.state).toBe(s); | |
| 24 | expect(r.event.type).toBe('refused'); | |
| 25 | expect(r.event.reason).toContain('other bank'); | |
| 26 | expect(cross(s, 'cow').event.type).toBe('refused'); | |
| 27 | expect(cross(s, ['goose', 'goose']).event.type).toBe('refused'); | |
| 28 | }); | |
| 29 | ||
| 30 | test('a one-seat ferry refuses two passengers', () => { | |
| 31 | expect(cross(newGame(0), ['fox', 'goose']).event.type).toBe('refused'); | |
| 32 | }); | |
| 33 | ||
| 34 | test('leaving fox with goose loses and names who was eaten', () => { | |
| 35 | const s = cross(newGame(0), 'grain').state; | |
| 36 | expect(s.over).toBe(true); | |
| 37 | expect(s.outcome).toBe('lost'); | |
| 38 | expect(s.eaten).toEqual({ hunter: 'fox', prey: 'goose' }); | |
| 39 | expect(s.line).toContain('fox ate the goose'); | |
| 40 | }); | |
| 41 | ||
| 42 | test('leaving goose with grain loses', () => { | |
| 43 | const s = cross(newGame(0), 'fox').state; | |
| 44 | expect(s.outcome).toBe('lost'); | |
| 45 | expect(s.eaten).toEqual({ hunter: 'goose', prey: 'grain' }); | |
| 46 | expect(s.line).toContain('goose ate the grain'); | |
| 47 | }); | |
| 48 | ||
| 49 | test('leaving the dog with the fox loses on level two', () => { | |
| 50 | const s = cross(newGame(1), ['goose', 'grain']).state; | |
| 51 | expect(s.eaten).toEqual({ hunter: 'dog', prey: 'fox' }); | |
| 52 | }); | |
| 53 | ||
| 54 | test('a finished round takes no more moves', () => { | |
| 55 | const s = cross(newGame(0), 'grain').state; | |
| 56 | expect(cross(s, null).state).toBe(s); | |
| 57 | expect(cross(s, null).event.type).toBe('refused'); | |
| 58 | }); | |
| 59 | ||
| 60 | test('the classic round is won at par with a report against par', () => { | |
| 61 | const s = play(newGame(0), solve(0)); | |
| 62 | expect(s.outcome).toBe('won'); | |
| 63 | expect(s.crossings).toBe(7); | |
| 64 | expect(report(s)).toMatchObject({ outcome: 'won', crossings: 7, par: 7 }); | |
| 65 | expect(s.line).toContain('7 of par 7'); | |
| 66 | expect(report(newGame(0))).toBe(null); | |
| 67 | }); | |
| 68 | ||
| 69 | test('a win over par says how far over', () => { | |
| 70 | const s = play(newGame(0), ['goose', 'goose', ...solve(0)]); // over and straight back wastes two crossings | |
| 71 | expect(s.outcome).toBe('won'); | |
| 72 | expect(s.crossings).toBe(9); | |
| 73 | expect(s.line).toContain('2 over'); | |
| 74 | }); | |
| 75 | ||
| 76 | test('the limit loss: running out of crossings loses with nobody eaten', () => { | |
| 77 | const s = play(newGame(2), [['dog', 'goose'], null, 'grain', 'goose', 'goose']); | |
| 78 | expect(s.crossings).toBe(LEVELS[2].limit); | |
| 79 | expect(s.outcome).toBe('lost'); | |
| 80 | expect(s.eaten).toBe(null); | |
| 81 | expect(s.line).toContain('out of crossings'); | |
| 82 | }); | |
| 83 | ||
| 84 | test('winning on the last allowed crossing is a win, not a limit loss', () => { | |
| 85 | const s = play(newGame(2), solve(2)); | |
| 86 | expect(s.outcome).toBe('won'); | |
| 87 | }); | |
| 88 | ||
| 89 | test('three levels, each with a par that is solvable and fewest', () => { | |
| 90 | expect(LEVELS.length).toBe(3); | |
| 91 | LEVELS.forEach((L, i) => { | |
| 92 | const path = solve(i); | |
| 93 | expect(path).not.toBe(null); | |
| 94 | expect(path.length).toBe(L.par); // BFS finds the fewest, so par is exactly reachable | |
| 95 | const s = play(newGame(i), path); | |
| 96 | expect(s.outcome).toBe('won'); | |
| 97 | expect(s.crossings).toBe(L.par); | |
| 98 | }); | |
| 99 | expect(LEVELS[1].passengers).toContain('dog'); | |
| 100 | expect(LEVELS[1].seats).toBe(2); | |
| 101 | expect(LEVELS[2].limit).not.toBe(null); | |
| 102 | expect(LEVELS[2].limit).toBeGreaterThanOrEqual(LEVELS[2].par); | |
| 103 | }); | |
| 104 | ||
| 105 | test('again starts a fresh round of the same level only when finished', () => { | |
| 106 | expect(again(newGame(1))).toBe(null); | |
| 107 | const lost = cross(newGame(1), ['goose', 'grain']).state; | |
| 108 | expect(again(lost)).toEqual(newGame(1)); | |
| 109 | }); |
new above
offline