From c391c68234fc195a768b67f8dd5eeaf76b675733 Mon Sep 17 00:00:00 2001 From: Andrew Hyndman Date: Mon, 4 Sep 2023 18:51:23 -0400 Subject: [PATCH] New projection: enumerateClues --- src/projections.ts | 25 +++++++++++++++++++++++++ test/projections.test.ts | 36 +++++++++++++++++++++++++++++++++++- 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/src/projections.ts b/src/projections.ts index 5190ba2..e52a064 100644 --- a/src/projections.ts +++ b/src/projections.ts @@ -74,6 +74,31 @@ export function getFileEncoding(puzzle: Puzzle): ENCODING { return guessFileEncodingFromVersion(puzzle.fileVersion); } +export function enumerateClues(puzzle: Pick): { + across: { number: number; clue: string }[]; + down: { number: number; clue: string }[]; +} { + const { clues, solution } = puzzle; + let clueNumber = 0; + const clueQueue = clues.slice(); + const across: { number: number; clue: string }[] = []; + const down: { number: number; clue: string }[] = []; + + [...solution].forEach((square, i) => { + const hasAcrossClue = squareNeedsAcrossClue(puzzle, i); + const hasDownClue = squareNeedsDownClue(puzzle, i); + if (hasAcrossClue || hasDownClue) clueNumber += 1; + if (hasAcrossClue) { + across.push({ number: clueNumber, clue: clueQueue.shift()! }); + } + if (hasDownClue) { + down.push({ number: clueNumber, clue: clueQueue.shift()! }); + } + }); + + return { across, down }; +} + export function gridNumbering(puzzle: Pick): (number | undefined)[] { const { solution } = puzzle; let clueNumber = 0; diff --git a/test/projections.test.ts b/test/projections.test.ts index c97f8e6..4a8e990 100644 --- a/test/projections.test.ts +++ b/test/projections.test.ts @@ -1,5 +1,5 @@ import { Puzzle } from '../src'; -import { gridNumbering, isCorrect } from '../src/projections'; +import { enumerateClues, gridNumbering, isCorrect } from '../src/projections'; const MINIMAL_PUZZLE: Puzzle = { height: 2, @@ -9,6 +9,40 @@ const MINIMAL_PUZZLE: Puzzle = { }; describe('projections', () => { + describe('enumerateClues', () => { + it('splits and numbers across & down clues', () => { + expect(enumerateClues(MINIMAL_PUZZLE)).toEqual({ + across: [ + { number: 1, clue: '1A' }, + { number: 3, clue: '3A' }, + ], + down: [ + { number: 1, clue: '1D' }, + { number: 2, clue: '2D' }, + ], + }); + }); + + it('handles black squares correctly', () => { + const puzzle = { + width: 3, + solution: 'A.CDEFGHI', + clues: ['1 down', '2 down', '3 across', '4 down', '5 across'], + }; + expect(enumerateClues(puzzle)).toEqual({ + across: [ + { number: 3, clue: '3 across' }, + { number: 5, clue: '5 across' }, + ], + down: [ + { number: 1, clue: '1 down' }, + { number: 2, clue: '2 down' }, + { number: 4, clue: '4 down' }, + ], + }); + }); + }); + describe('gridNumbering', () => { it('assigns numbers to squares', () => { const puzzle = {