{"id":16664197,"url":"https://github.com/jmitchell/backtrex","last_synced_at":"2025-03-21T17:32:18.912Z","repository":{"id":57479207,"uuid":"78806579","full_name":"jmitchell/backtrex","owner":"jmitchell","description":"Backtracking behaviour to solve discrete problems by brute force","archived":false,"fork":false,"pushed_at":"2017-02-05T19:31:18.000Z","size":48,"stargazers_count":26,"open_issues_count":9,"forks_count":2,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-10-13T10:44:30.990Z","etag":null,"topics":["backtracking","brute-force","elixir","solver"],"latest_commit_sha":null,"homepage":"","language":"Elixir","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jmitchell.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-01-13T02:17:51.000Z","updated_at":"2024-10-08T06:06:57.000Z","dependencies_parsed_at":"2022-09-17T04:41:56.788Z","dependency_job_id":null,"html_url":"https://github.com/jmitchell/backtrex","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jmitchell%2Fbacktrex","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jmitchell%2Fbacktrex/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jmitchell%2Fbacktrex/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jmitchell%2Fbacktrex/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jmitchell","download_url":"https://codeload.github.com/jmitchell/backtrex/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221817226,"owners_count":16885479,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["backtracking","brute-force","elixir","solver"],"created_at":"2024-10-12T10:44:14.781Z","updated_at":"2024-10-28T10:31:40.576Z","avatar_url":"https://github.com/jmitchell.png","language":"Elixir","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Backtrex [![Build Status](https://travis-ci.org/jmitchell/backtrex.svg?branch=master)](https://travis-ci.org/jmitchell/backtrex)\n\nLogic puzzles and similar problems can be solved by exploring the\nspace of possible solutions. By enumerating potential answers to open\nquestions like \"what should go in this cell?\" or \"which direction\nshould I go now?\", checking whether a given set of those answers is\nvalid, and revising answers whenever you reach an invalid state you'll\neventually find the solution (assuming one exists and the problem is\nfinite). This strategy is known\nas [backtracking](https://en.wikipedia.org/wiki/Backtracking) and has\na surprising range of applications. A few simple callbacks are all\nBacktrex needs to get to work.\n\n## Getting Started\n\nBacktrex is an Elixir project, and these directions assume you are\nusing the `mix` tool. However, it should be possible to use this\nproject in Erlang as well.\n\n### Installing\n\nAdd `backtrex` to your list of dependencies in `mix.exs`:\n\n```elixir\ndef deps do\n  [{:backtrex, \"~\u003e 0.1.2\"}]\nend\n```\n\nThen run `mix do deps.get`.\n\nBacktrex currently makes a lot of Logger.debug calls. To avoid seeing\nthem, add the following to `config/config.exs`:\n\n```elixir\nconfig :logger, level: :warn\n```\n\n### Usage\n\nSuppose you want to make a Sudoku solver, and you already have ways to\nrepresent, update, and validate puzzles. Here's roughly what the\nsolver, a Backtrex behaviour, would look like:\n\n```elixir\ndefmodule SudokuSolver do\n  use Backtrex\n  \n  def unknowns(puzzle) do\n    SudokuPuzzle.empty_cells(puzzle)\n  end\n  \n  def values(_puzzle, _cell), do: 1..9\n  \n  def assign(puzzle, cell, value) do\n    SudokuPuzzle.put_cell(puzzle, cell, value)\n  end\n\n  def valid?(puzzle) do\n    SudokuPuzzle.valid?(puzzle)\n  end\nend\n```\n\nIn return for implementing these callbacks, Backtrex gives you a\n`SudokuSolver.solve/1` function for free.\n\nThe documentation\nat [https://hexdocs.pm/backtrex](https://hexdocs.pm/backtrex) explains\nwhat Backtrex expects from these callbacks.\n\nSimilar Sudoku solver code is available\nin\n[`lib/examples/sudoku`](https://github.com/jmitchell/backtrex/tree/master/lib/examples/sudoku). For\nnow those modules are even shipped with the package. Try it out in\nyour project:\n\n```elixir\ndefmodule Sandbox do\n  alias Backtrex.Examples.Sudoku.Puzzle\n  alias Backtrex.Examples.Sudoku.Solver\n\n  def hello_backtrex do\n    {:ok, puzzle} = Puzzle.from_list([\n      [5,   3, :_, :_,  7, :_, :_, :_, :_],\n      [6,  :_, :_,  1,  9,  5, :_, :_, :_],\n      [:_,  9,  8, :_, :_, :_, :_,  6, :_],\n      [8,  :_, :_, :_,  6, :_, :_, :_,  3],\n      [4,  :_, :_,  8, :_,  3, :_, :_,  1],\n      [7,  :_, :_, :_,  2, :_, :_, :_,  6],\n      [:_,  6, :_, :_, :_, :_,  2,  8, :_],\n      [:_, :_, :_,  4,  1,  9, :_, :_,  5],\n      [:_, :_, :_, :_,  8, :_, :_,  7,  9]])\n\n    {:ok, expected_solution} = Puzzle.from_list([\n      [5, 3, 4, 6, 7, 8, 9, 1, 2],\n      [6, 7, 2, 1, 9, 5, 3, 4, 8],\n      [1, 9, 8, 3, 4, 2, 5, 6, 7],\n      [8, 5, 9, 7, 6, 1, 4, 2, 3],\n      [4, 2, 6, 8, 5, 3, 7, 9, 1],\n      [7, 1, 3, 9, 2, 4, 8, 5, 6],\n      [9, 6, 1, 5, 3, 7, 2, 8, 4],\n      [2, 8, 7, 4, 1, 9, 6, 3, 5],\n      [3, 4, 5, 2, 8, 6, 1, 7, 9]])\n\n    {:ok, :solution, solution} = puzzle |\u003e Solver.solve\n\n    solution == expected_solution\n  end\nend\n```\n\n`Sandbox.hello_backtrex/0` should return true within 5 seconds or\nso.\n\n## Other applications\n\nSudoku makes for a good introductory demo, but it doesn't showcase the\nflexibility of Backtrex's design. Here's some other applications I\nsuspect it could handle.\n\n- incomplete information\n  - corn maze of unknown size; visibility limited to what's nearby.\n- don't know until you get there\n  - finding a string of 5 characters that hashes to a certain value.\n- programming language features\n  - pattern matching engine\n  - type checker\n\n## License\n\nBacktrex is licensed under Apache 2.0. See\nthe\n[LICENSE file](https://github.com/jmitchell/backtrex/blob/master/LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjmitchell%2Fbacktrex","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjmitchell%2Fbacktrex","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjmitchell%2Fbacktrex/lists"}