Skip to content

Instantly share code, notes, and snippets.

@g00glen00b
Created January 26, 2022 21:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save g00glen00b/cdb059d0a5b3046484887fc2c6ba6fd1 to your computer and use it in GitHub Desktop.
Save g00glen00b/cdb059d0a5b3046484887fc2c6ba6fd1 to your computer and use it in GitHub Desktop.
Cheats binarypuzzle.com
function getInput() {
const cells = [...document.querySelectorAll('.puzzlecel')];
const result = [];
cells.forEach(cell => {
const {row, column} = getCoordinates(cell);
if (result[row] == null) result[row] = [];
result[row][column] = cell;
});
return result;
}
function rotate(input) {
return input[0].map((_, index) => input.map(row => row[index]).reverse());
}
function getCoordinates(cell) {
const [_, rowStr, columnStr] = cell.id.split('_');
const row = parseInt(rowStr) - 1;
const column = parseInt(columnStr) - 1;
return {row, column};
}
function setValue(cell, value) {
const {row, column} = getCoordinates(cell);
CelClick(row + 1, column + 1);
if (value === 1) CelClick(row + 1, column + 1);
}
function groupByValue(cells) {
let zero = [];
let one = [];
let unknown = [];
cells.forEach(cell => {
if (cell.innerText === '0') zero.push(cell);
else if (cell.innerText === '1') one.push(cell);
else unknown.push(cell);
});
return {zero, one, unknown};
}
function calculateAdjacentValues(row, cellIndex) {
const nextValue = cellIndex < row.length - 1 ? row[cellIndex + 1].innerText : null;
const secondNextValue = cellIndex < row.length - 2 ? row[cellIndex + 2].innerText : null;
const previousValue = cellIndex > 0 ? row[cellIndex - 1].innerText : null;
const secondPreviousValue = cellIndex > 1 ? row[cellIndex - 2].innerText : null;
return {nextValue, secondNextValue, previousValue, secondPreviousValue};
}
function calculateNewValue({nextValue, secondNextValue, previousValue, secondPreviousValue}) {
if (nextValue === previousValue && nextValue === '0') return 1;
else if (nextValue === previousValue && nextValue === '1') return 0;
else if (nextValue === secondNextValue && nextValue === '0') return 1;
else if (nextValue === secondNextValue && nextValue === '1') return 0;
else if (previousValue === secondPreviousValue && previousValue === '0') return 1;
else if (previousValue === secondPreviousValue && previousValue === '1') return 0;
else return null;
}
function solveAdjacentValues(row) {
return row.some((cell, cellIndex) => {
if (cell.innerText === '') {
const adjacentValues = calculateAdjacentValues(row, cellIndex);
const newValue = calculateNewValue(adjacentValues);
if (newValue != null) {
setValue(cell, newValue);
return true;
} else {
return false;
}
}
});
}
function solveMissingValues(row) {
const {zero, one, unknown} = groupByValue(row);
if (zero.length === row.length / 2 && unknown.length > 0) {
setValue(unknown[0], 1);
return true;
} else if (one.length === row.length / 2 && unknown.length > 0) {
setValue(unknown[0], 0);
return true;
} else {
return false;
}
}
function solveRowIteration(row) {
return solveAdjacentValues(row) || solveMissingValues(row);
}
function solveIteration(input) {
return input.some(solveRowIteration);
}
function solve() {
let input = getInput();
const inputBothDirections = [...input, ...rotate(input)];
const interval = setInterval(() => {
const changed = solveIteration(inputBothDirections);
if (!changed) clearInterval(interval);
}, 100);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment