solve_board: Solve a Lights Out board

View source: R/solve.R

solve_boardR Documentation

Solve a Lights Out board

Description

Given a Lights Out board, find the set of lights that need to be pressed in order to solve the board. If no solution is possible, an error is thrown.

Usage

solve_board(board)

Arguments

board

A lightsout board object.

Details

There are a few algorithms for solving Lights Out puzzles. This function implements the Gaussian Elimination technique, which does not guarantee the minimum number of steps. Therefore, some steps in the given solution may be redundant.

If you are interested, there are many resources online outlining the exact details of how this technique works, and what the other solving strategies are.

Value

A matrix with the same dimensions as the input board, with a 1 in every position that requires a press to solve to the board. Note that the order of the light presses does not matter.

See Also

new_board random_board play is_solvable is_solved

Examples

# Create an empty 5x5 board, press two lights, and then see that the solution
# tells us to press the same lights in order to solve the board.
board <- empty_board(5) %>% play(3, 2) %>% play(4, 1)
board
solution <- solve_board(board)
solution
board <- play(board, matrix = solution)
is_solved(board)

lightsout documentation built on Aug. 21, 2023, 9:07 a.m.