{"id":31781219,"url":"https://github.com/giuse/nqueens","last_synced_at":"2025-10-10T08:34:38.185Z","repository":{"id":24893989,"uuid":"28310296","full_name":"giuse/nqueens","owner":"giuse","description":"Ruby solver for the n-queens problem","archived":false,"fork":false,"pushed_at":"2014-12-22T17:48:00.000Z","size":184,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2023-08-02T14:08:29.116Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/giuse.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":"2014-12-21T19:26:28.000Z","updated_at":"2023-08-02T14:08:29.117Z","dependencies_parsed_at":"2022-08-22T20:31:38.627Z","dependency_job_id":null,"html_url":"https://github.com/giuse/nqueens","commit_stats":null,"previous_names":[],"tags_count":0,"template":null,"template_full_name":null,"purl":"pkg:github/giuse/nqueens","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/giuse%2Fnqueens","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/giuse%2Fnqueens/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/giuse%2Fnqueens/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/giuse%2Fnqueens/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/giuse","download_url":"https://codeload.github.com/giuse/nqueens/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/giuse%2Fnqueens/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279003296,"owners_count":26083555,"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","status":"online","status_checked_at":"2025-10-10T02:00:06.843Z","response_time":62,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":[],"created_at":"2025-10-10T08:31:39.865Z","updated_at":"2025-10-10T08:34:38.171Z","avatar_url":"https://github.com/giuse.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# N-Queens puzzle solver - version 4.2\n\nPlaces `n` chess queens on an `n x n` chessboard without conflicts.\n\nGeneralization of the classic Eight Queens Puzzle ([wiki](http://en.wikipedia.org/wiki/Eight_queens_puzzle))\n\n### Approach\n\nThis puzzle can be seen as enforcing two conditions over a set of `n` coordinates on a `n`-by-`n` chessboard:\n- There should be no orthogonal crossing (no two coords sharing row/column), \n- There should be no diagonal crossing (no two coords on same diagonal).\n\nI satisfy the first by building candidate solutions from permutations of rows and columns, to grant their uniqueness. I verify the second condition by rotating the reference system by PI/4 radians, then checking the common rows/columns.\n\n### Optimization\n\nExplicitly searching a permutation space is extremely inefficient. In order to reduce its size, I fix the order of the queens description, without loss of generality: the first queen is always on the first row, the second on the second row, etc. This means I can describe a solution as a permutation of column positions, rather than the (more common) approach of row/column permutations.\n\nThis method is extremely efficient in both CPU and RAM usage. My laptop finds all solutions (including mirrored) for the classic 8 queens in half a second, and 9 queens in less than 6 seconds, with a ram occupation of few KB since each solution is evaluated (and usually discarded) upon construction.\n\n... And this is a good example of why I prefer approaching a hard problem with a high-level language and optimize the algorithm, rather than lose myself in making a highly optimized language solve a complex problem.\n\n### Further work\n\nI'm very tempted to bring it to 3D-chess :) only the terminal pretty printing won't cut it out anymore.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgiuse%2Fnqueens","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgiuse%2Fnqueens","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgiuse%2Fnqueens/lists"}