Debian Codegraph @

rhino/testsrc/tests/js1_8/genexps/regress-380237-01.js

raw symbols SCIP
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 var gTestfile = 'regress-380237-01.js';
7
8 //-----------------------------------------------------------------------------
9 var BUGNUMBER = 380237;
10 var summary = 'Generator expressions - sudoku';
11 var actual = '';
12 var expect = '';
13
14
15 //-----------------------------------------------------------------------------
16 test();
17 //-----------------------------------------------------------------------------
18
19 function test()
20 {
21 enterFunc ('test');
22 printBugNumber(BUGNUMBER);
23 printStatus (summary);
24
25 if (this.version) version(180);
26
27 // XXX should be standard (and named clone, after Java?)
28 Object.prototype.copy = function () {
29 let o = {}
30 for (let i in this)
31 o[i] = this[i]
32 return o
33 }
34
35 // Make arrays and strings act more like Python lists by iterating their values, not their keys.
36 Array.prototype.__iterator__ = String.prototype.__iterator__ = function () {
37 for (let i = 0; i < this.length; i++)
38 yield this[i]
39 }
40
41 // Containment testing for arrays and strings that should be coherent with their __iterator__.
42 Array.prototype.contains = String.prototype.contains = function (e) {
43 return this.indexOf(e) != -1
44 }
45
46 Array.prototype.repeat = String.prototype.repeat = function (n) {
47 let s = this.constructor()
48 for (let i = 0; i < n; i++)
49 s = s.concat(this)
50 return s
51 }
52
53 String.prototype.center = function (w) {
54 let n = this.length
55 if (w <= n)
56 return this
57 let m = Math.floor((w - n) / 2)
58 return ' '.repeat(m) + this + ' '.repeat(w - n - m)
59 }
60
61 Array.prototype.toString = Array.prototype.toSource
62 Object.prototype.toString = Object.prototype.toSource
63
64 // XXX thought spurred by the next two functions: array extras should default to identity function
65
66 function all(seq) {
67 for (let e in seq)
68 if (!e)
69 return false
70 return true
71 }
72
73 function some(seq) {
74 for (let e in seq)
75 if (e)
76 return e
77 return false
78 }
79
80 function cross(A, B) {
81 return [a+b for (a in A) for (b in B)]
82 }
83
84 function dict(A) {
85 let d = {}
86 for (let e in A)
87 d[e[0]] = e[1]
88 return d
89 }
90
91 function set(A) {
92 let s = []
93 for (let e in A)
94 if (!s.contains(e))
95 s.push(e)
96 return s
97 }
98
99 function zip(A, B) {
100 let z = []
101 let n = Math.min(A.length, B.length)
102 for (let i = 0; i < n; i++)
103 z.push([A[i], B[i]])
104 return z
105 }
106
107 rows = 'ABCDEFGHI'
108 cols = '123456789'
109 digits = '123456789'
110 squares = cross(rows, cols)
111 unitlist = [cross(rows, c) for (c in cols)]
112 .concat([cross(r, cols) for (r in rows)])
113 .concat([cross(rs, cs) for (rs in ['ABC','DEF','GHI']) for (cs in ['123','456','789'])])
114 units = dict([s, [u for (u in unitlist) if (u.contains(s))]]
115 for (s in squares))
116 peers = dict([s, set([s2 for (u in units[s]) for (s2 in u) if (s2 != s)])]
117 for (s in squares))
118
119 // Given a string of 81 digits (or . or 0 or -), return a dict of {cell:values}.
120 function parse_grid(grid) {
121 grid = [c for (c in grid) if ('0.-123456789'.contains(c))]
122 let values = dict([s, digits] for (s in squares))
123
124 for (let [s, d] in zip(squares, grid))
125 if (digits.contains(d) && !assign(values, s, d))
126 return false
127 return values
128 }
129
130 // Eliminate all the other values (except d) from values[s] and propagate.
131 function assign(values, s, d) {
132 if (all(eliminate(values, s, d2) for (d2 in values[s]) if (d2 != d)))
133 return values
134 return false
135 }
136
137 // Eliminate d from values[s]; propagate when values or places <= 2.
138 function eliminate(values, s, d) {
139 if (!values[s].contains(d))
140 return values // Already eliminated
141 values[s] = values[s].replace(d, '')
142 if (values[s].length == 0)
143 return false // Contradiction: removed last value
144 if (values[s].length == 1) {
145 // If there is only one value (d2) left in square, remove it from peers
146 let d2 = values[s][0]
147 if (!all(eliminate(values, s2, d2) for (s2 in peers[s])))
148 return false
149 }
150 // Now check the places where d appears in the units of s
151 for (let u in units[s]) {
152 let dplaces = [s for (s in u) if (values[s].contains(d))]
153 if (dplaces.length == 0)
154 return false
155 if (dplaces.length == 1)
156 // d can only be in one place in unit; assign it there
157 if (!assign(values, dplaces[0], d))
158 return false
159 }
160 return values
161 }
162
163 // Used for debugging.
164 function print_board(values) {
165 let width = 1 + Math.max.apply(Math, [values[s].length for (s in squares)])
166 let line = '\n' + ['-'.repeat(width*3)].repeat(3).join('+')
167 for (let r in rows)
168 print([values[r+c].center(width) + ('36'.contains(c) && '|' || '')
169 for (c in cols)].join('') + ('CF'.contains(r) && line || ''))
170 print('\n')
171 }
172
173 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.."
174
175 print_board(parse_grid(easy))
176
177 // Using depth-first search and constraint propagation, try all possible values.
178 function search(values) {
179 if (!values)
180 return false // Failed earlier
181 if (all(values[s].length == 1 for (s in squares)))
182 return values // Solved!
183
184 // Choose the unfilled square s with the fewest possibilities
185 // XXX Math.min etc. should work with generator expressions and other iterators
186 // XXX Math.min etc. should work on arrays (lists or tuples in Python) as well as numbers
187 let a = [values[s].length + s for (s in squares) if (values[s].length > 1)].sort()
188 let s = a[0].slice(-2)
189
190 return some(search(assign(values.copy(), s, d)) for (d in values[s]))
191 }
192
193 hard = '4.....8.5.3..........7......2.....6.....8.4......1.......6.3.7.5..2.....1.4......'
194
195 print_board(search(parse_grid(hard)))
196
197 delete Object.prototype.copy;
198
199 reportCompare(expect, actual, summary);
200
201 exitFunc ('test');
202 }