dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
2.0 KiB
2.0 KiB
id, title, challengeType, dashedName
| id | title | challengeType | dashedName |
|---|---|---|---|
| 6a26df95efa55a2524399743 | Challenge 362: Nonogram Validator | 28 | challenge-362 |
--description--
Given an array of clue numbers and an array of cells, determine whether the cells satisfy the nonogram clue.
- The clue is an array of numbers representing the lengths of consecutive filled cells, in order. For example, a clue of
[3, 2]means there should be 3 consecutive filled cells followed by 2 consecutive filled cells, separated by at least one empty cell. - The row is an array of 1s (filled) and 0s (empty).
--hints--
isValidNonogram([3, 2], [1, 1, 1, 0, 1, 1]) should return true.
assert.isTrue(isValidNonogram([3, 2], [1, 1, 1, 0, 1, 1]));
isValidNonogram([3, 2], [0, 1, 1, 1, 1, 1]) should return false.
assert.isFalse(isValidNonogram([3, 2], [0, 1, 1, 1, 1, 1]));
isValidNonogram([1, 1, 1, 1], [1, 0, 1, 0, 1, 0, 1, 0, 1]) should return false.
assert.isFalse(isValidNonogram([1, 1, 1, 1], [1, 0, 1, 0, 1, 0, 1, 0, 1]));
isValidNonogram([1, 1, 1, 1], [0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0]) should return true.
assert.isTrue(isValidNonogram([1, 1, 1, 1], [0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0]));
isValidNonogram([3, 2, 3], [0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0]) should return true.
assert.isTrue(isValidNonogram([3, 2, 3], [0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0]));
isValidNonogram([3, 2, 3], [0, 0, 0, 1, 0, 0, 1, 0, 0, 0]) should return false.
assert.isFalse(isValidNonogram([3, 2, 3], [0, 0, 0, 1, 0, 0, 1, 0, 0, 0]));
--seed--
--seed-contents--
function isValidNonogram(clue, cells) {
return clue;
}
--solutions--
function isValidNonogram(clue, cells) {
const runs = [];
let count = 0;
for (let i = 0; i <= cells.length; i++) {
if (cells[i] === 1) {
count++;
} else if (count > 0) {
runs.push(count);
count = 0;
}
}
if (runs.length !== clue.length) return false;
return runs.every((run, i) => run === clue[i]);
}