24 Game Solver

The 24 game

24 (game) - Wikipedia

The 24 puzzle is an arithmetical puzzle in which the objective is to find a way to manipulate four integers so that the end result is 24. For example, for the numbers 4, 7, 8, 8, a possible solution is (7 − (8 ÷ 8)) × 4 = 24

Cases outside the original game

This solver can handle cases outside the original game, like floating point numbers, negative numbers, and more than 4 cards.

Usage

import 'package:solve_24_game/solve_24_game.dart';

void main() {
  final solutions = solve([1, 2, 3, 4], 24);

  for (final solution in solutions) {
    print(solution);
  }
}

Cli usage

Activate: dart pub global activate solve_24_game

Usage: solve_24_game <number> <number> <number> <number> [= <expected result>]

Example: solve_24_game 1 2 3 4 = 24

Using pub: dart pub global run solve_24_game 1 2 2 = 5

Examples outside the original game

This solver can handle cases outside the original game, like floating point numbers, negative numbers, and more than 4 cards.

Example 1

Input: 10 20 30 40 50

Target: 500

Solutions:

10 * 20 - 30 * (40 - 50) = 500
(10 * 30 - 50) / 20 * 40 = 500
(10 * 30 - 50) * 40 / 20 = 500
(10 * 30 - 50) / 20 / 40 = 500
(10 * 30 - 50) / 20 / 40 = 500
10 * 30 - 20 * (40 - 50) = 500
...

Example 2

Input: 6.5 7.3 8.4

Target: 22.2

Solutions:

(6.5 + 8.4) + 7.3 = 22.2
6.5 + (7.3 + 8.4) = 22.2