Skip to content

Commit

Permalink
New projection: enumerateClues
Browse files Browse the repository at this point in the history
  • Loading branch information
ajhyndman committed Sep 4, 2023
1 parent f54e0c3 commit c391c68
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
25 changes: 25 additions & 0 deletions src/projections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,31 @@ export function getFileEncoding(puzzle: Puzzle): ENCODING {
return guessFileEncodingFromVersion(puzzle.fileVersion);
}

export function enumerateClues(puzzle: Pick<Puzzle, 'solution' | 'clues' | 'width'>): {
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<Puzzle, 'solution' | 'width'>): (number | undefined)[] {
const { solution } = puzzle;
let clueNumber = 0;
Expand Down
36 changes: 35 additions & 1 deletion test/projections.test.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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 = {
Expand Down

0 comments on commit c391c68

Please sign in to comment.