A simple webapp (SPA) for solving sudoku puzzles written in Gleam using the Lustre framework
This is the web (SPA) version of the command-line tool for solving Sudoku puzzles written in Gleam, using the Lustre framework. At the same time, both are the almost direct translation of the Elixir version that I made of Peter Norvig's algorithm for solving sudokus. This computer scientist developed his algorithm in Python, based on 2 concepts: constraint propagation and depth-first search (a sort of 'sophisticated' brute force). If you have any difficulty understanding it, you can find more details here.
You can try the application at this link. Simply enter the puzzle you want to solve as an 81-character string (9x9 = 81) with no spaces, where empty squares must be represented by periods '.' or zeros '0' and the rest by digits from '1' to '9', something like this: ...8...5..8..3..2.1....4.........8.663.....419.5.........6....3.9..5..1..17..89.... A string entered with a different number of characters or prohibited characters will generate a warning message. Pressing the Import button will display a grid representation of the puzzle you entered, along with the solved Sudoku. The Reset button will return you to the input screen so you can enter a new puzzle.
You can get the strings you need to pass as an entry from this
site, where you can find Sudoku puzzles with all levels of difficulty.
-
Working on the code:
With Gleam installed as explained here (and
Erlangand itsBEAM VM), you only need to run the following command in a terminal open in the project folder to start the application in development mode:gleam add --dev lustre_dev_tools
This will download the dependencies, compile the project, and start a development server. In your browser, go to http://localhost:1234 and you will be able to see the application. Any changes you make to the code using your text editor will cause the browser to reload and display the changes.
The lustre_dev_tools development server watches your filesystem for changes to your gleam code and can automatically reload the browser. For
Linuxusers, this requires inotify-tools be installed. If you do not or cannot install this, the development server will still run but it will not watch your files for changes. -
Compiling the code for production deployment:
If, after making changes to the code, you decide to test the
SPA applicationon astatic file server, simply run the following command in the project folder opened in a terminal:gleam run -m lustre/dev build sudoku_gleam_webapp --minify
This will compile (or rather transpile) the
JavaScriptproject, generating anHTMLskeleton and minifying both theCSSand JavaScript files, saving everything in a/distfolder at the project root. You can find more details about configuring the build for development and production here and here.
- Official website: https://gleam.run/
- Guides: https://hexdocs.pm/gleam_stdlib/index.html
- Lustre framework: https://hexdocs.pm/lustre/index.html
- Lustre Dev Tools: https://hexdocs.pm/lustre_dev_tools/index.html
- Community: https://discord.com/invite/Fm8Pwmy
- Gleam discussions on Github: https://github.com/gleam-lang/gleam/discussions