Skip to main content

Riddle solver algorithms for the NYT Digits game

Project description

nyt-digits-solver

Puzzle solver for the NYT Digits game (no longer active).

Given a set of numbers and a target, finds the sequence of arithmetic operations (+, -, *, /) that produces the target. Also lets you explore all values reachable from a given input, with the steps to get there.

Installation

pip install nyt-digits-solver

Usage

solve

Returns a list of solutions. Each solution is a list of Step(numbers, operation) where operation is the step that produced that state (None for the initial state).

import nyt_digits_solver as ds

solutions = ds.solve([1, 2, 3], target=6)
# [
#   [
#     Step(numbers=[1, 2, 3], operation=None),
#     Step(numbers=[3, 3],    operation='2+1'),
#     Step(numbers=[6],       operation='3+3'),
#   ]
# ]

for step in solutions[0]:
    if step.operation:
        print(f"{step.operation}{step.numbers}")
# 2+1 → [3, 3]
# 3+3 → [6]

Find all solutions instead of just the first:

all_solutions = ds.solve([1, 2, 3, 4, 5, 25], target=452, how_many_sols=None)

explore

Returns a dict mapping every reachable value to the shortest list of Steps that produces it.

result = ds.explore([2, 3])
# {
#   5: [Step(numbers=[2, 3], operation=None), Step(numbers=[5], operation='3+2')],
#   1: [Step(numbers=[2, 3], operation=None), Step(numbers=[1], operation='3-2')],
#   6: [Step(numbers=[2, 3], operation=None), Step(numbers=[6], operation='3*2')],
# }

# Check if a target is reachable
if 452 in ds.explore([1, 2, 3, 4, 5, 25]):
    print("reachable!")

# Get the steps to a specific value
steps = ds.explore([1, 2, 3, 4, 5, 25])[452]
for step in steps:
    if step.operation:
        print(f"{step.operation}{step.numbers}")

CLI

python -m nyt_digits_solver.main -n 1,2,3 -t 6
# Found 1 solution. Took 0.0 seconds.
# Start: [1, 2, 3]
# 2+1 → [3, 3]
# 3+3 → [6]

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nyt_digits_solver-0.2.0.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nyt_digits_solver-0.2.0-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file nyt_digits_solver-0.2.0.tar.gz.

File metadata

  • Download URL: nyt_digits_solver-0.2.0.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for nyt_digits_solver-0.2.0.tar.gz
Algorithm Hash digest
SHA256 e84320aab8a03bfd1b88c7aacd884e91d9238cdaa78c9e2c9c06ab65b44e8cf8
MD5 1862addb495100a5e57b77b47200170e
BLAKE2b-256 93f2d07e14e18215ca16e61828f161ce1c5c444c270322b0ea9ebbb5f60009fa

See more details on using hashes here.

File details

Details for the file nyt_digits_solver-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for nyt_digits_solver-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f5703fe277d1224b18508cf9118a18e6366f430dcc5cda47a9a6cda7cf664534
MD5 125b0a199ad99ba421a77c3b3844f65a
BLAKE2b-256 bdcffe0652c3aa9c32e570216069451b3b0e3b8c622072d5082834a9b6d657be

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page