1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is JavaScript Engine testing utilities.
17 * The Initial Developer of the Original Code is
19 * Portions created by the Initial Developer are Copyright (C) 2007
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s): Brendan
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
39 //-----------------------------------------------------------------------------
40 var BUGNUMBER = 380237;
41 var summary = 'Generator expressions - sudoku';
46 //-----------------------------------------------------------------------------
48 //-----------------------------------------------------------------------------
53 printBugNumber(BUGNUMBER);
54 printStatus (summary);
56 if (this.version) version(180);
58 // XXX should be standard (and named clone, after Java?)
59 Object.prototype.copy = function () {
66 // Make arrays and strings act more like Python lists by iterating their values, not their keys.
67 Array.prototype.__iterator__ = String.prototype.__iterator__ = function () {
68 for (let i = 0; i < this.length; i++)
72 // Containment testing for arrays and strings that should be coherent with their __iterator__.
73 Array.prototype.contains = String.prototype.contains = function (e) {
74 return this.indexOf(e) != -1
77 Array.prototype.repeat = String.prototype.repeat = function (n) {
78 let s = this.constructor()
79 for (let i = 0; i < n; i++)
84 String.prototype.center = function (w) {
88 let m = Math.floor((w - n) / 2)
89 return ' '.repeat(m) + this + ' '.repeat(w - n - m)
92 Array.prototype.toString = Array.prototype.toSource
93 Object.prototype.toString = Object.prototype.toSource
95 // XXX thought spurred by the next two functions: array extras should default to identity function
111 function cross(A, B) {
112 return [a+b for (a in A) for (b in B)]
132 let n = Math.min(A.length, B.length)
133 for (let i = 0; i < n; i++)
141 squares = cross(rows, cols)
142 unitlist = [cross(rows, c) for (c in cols)]
143 .concat([cross(r, cols) for (r in rows)])
144 .concat([cross(rs, cs) for (rs in ['ABC','DEF','GHI']) for (cs in ['123','456','789'])])
145 units = dict([s, [u for (u in unitlist) if (u.contains(s))]]
147 peers = dict([s, set([s2 for (u in units[s]) for (s2 in u) if (s2 != s)])]
150 // Given a string of 81 digits (or . or 0 or -), return a dict of {cell:values}.
151 function parse_grid(grid) {
152 grid = [c for (c in grid) if ('0.-123456789'.contains(c))]
153 let values = dict([s, digits] for (s in squares))
155 for (let [s, d] in zip(squares, grid))
156 if (digits.contains(d) && !assign(values, s, d))
161 // Eliminate all the other values (except d) from values[s] and propagate.
162 function assign(values, s, d) {
163 if (all(eliminate(values, s, d2) for (d2 in values[s]) if (d2 != d)))
168 // Eliminate d from values[s]; propagate when values or places <= 2.
169 function eliminate(values, s, d) {
170 if (!values[s].contains(d))
171 return values // Already eliminated
172 values[s] = values[s].replace(d, '')
173 if (values[s].length == 0)
174 return false // Contradiction: removed last value
175 if (values[s].length == 1) {
176 // If there is only one value (d2) left in square, remove it from peers
177 let d2 = values[s][0]
178 if (!all(eliminate(values, s2, d2) for (s2 in peers[s])))
181 // Now check the places where d appears in the units of s
182 for (let u in units[s]) {
183 let dplaces = [s for (s in u) if (values[s].contains(d))]
184 if (dplaces.length == 0)
186 if (dplaces.length == 1)
187 // d can only be in one place in unit; assign it there
188 if (!assign(values, dplaces[0], d))
194 // Used for debugging.
195 function print_board(values) {
196 let width = 1 + Math.max.apply(Math, [values[s].length for (s in squares)])
197 let line = '\n' + ['-'.repeat(width*3)].repeat(3).join('+')
199 print([values[r+c].center(width) + ('36'.contains(c) && '|' || '')
200 for (c in cols)].join('') + ('CF'.contains(r) && line || ''))
204 easy = "..3.2.6..9..3.5..1..18.64....81.29..7.......8..67.82....26.95..8..2.3..9..5.1.3.."
206 print_board(parse_grid(easy))
208 // Using depth-first search and constraint propagation, try all possible values.
209 function search(values) {
211 return false // Failed earlier
212 if (all(values[s].length == 1 for (s in squares)))
213 return values // Solved!
215 // Choose the unfilled square s with the fewest possibilities
216 // XXX Math.min etc. should work with generator expressions and other iterators
217 // XXX Math.min etc. should work on arrays (lists or tuples in Python) as well as numbers
218 let a = [values[s].length + s for (s in squares) if (values[s].length > 1)].sort()
219 let s = a[0].slice(-2)
221 return some(search(assign(values.copy(), s, d)) for (d in values[s]))
224 hard = '4.....8.5.3..........7......2.....6.....8.4......1.......6.3.7.5..2.....1.4......'
226 print_board(search(parse_grid(hard)))
228 delete Object.prototype.copy;
230 reportCompare(expect, actual, summary);