Created
November 2, 2021 19:19
-
-
Save vird/fdb484790f596eae232b1050e4bdcbc5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# sketch code, may require fixes | |
Logic = require 'logic-solver' | |
# small cell | |
rule_9_list = [] | |
for b_x in [0 ... 3] | |
for b_y in [0 ... 3] | |
for s_x in [0 ... 3] | |
for s_y in [0 ... 3] | |
list = [] | |
for i in [1 .. 9] | |
list.push "cell#{b_x}_#{b_y}_#{s_x}_#{s_y}_#{i}" | |
rule_9_list.push list | |
# cell | |
for i in [1 .. 9] | |
for b_x in [0 ... 3] | |
for b_y in [0 ... 3] | |
list = [] | |
for s_x in [0 ... 3] | |
for s_y in [0 ... 3] | |
list.push "cell#{b_x}_#{b_y}_#{s_x}_#{s_y}_#{i}" | |
rule_9_list.push list | |
# row | |
for i in [1 .. 9] | |
for b_x in [0 ... 3] | |
for s_x in [0 ... 3] | |
list = [] | |
for b_y in [0 ... 3] | |
for s_y in [0 ... 3] | |
list.push "cell#{b_x}_#{b_y}_#{s_x}_#{s_y}_#{i}" | |
rule_9_list.push list | |
# col | |
for i in [1 .. 9] | |
for b_y in [0 ... 3] | |
for s_y in [0 ... 3] | |
list = [] | |
for b_x in [0 ... 3] | |
for s_x in [0 ... 3] | |
list.push "cell#{b_x}_#{b_y}_#{s_x}_#{s_y}_#{i}" | |
rule_9_list.push list | |
class window.Sudoku | |
grid : [] | |
constructor : ()-> | |
@grid = [] | |
for b_x in [0 ... 3] | |
@grid.push b_row = [] | |
for b_y in [0 ... 3] | |
b_row.push s_cell = [] | |
for s_x in [0 ... 3] | |
s_cell.push s_col = [] | |
for s_y in [0 ... 3] | |
s_col.push undefined | |
clone : ()-> | |
ret = new Sudoku | |
ret.from_state state:@to_state() | |
ret | |
from_state: (athis)-> | |
for b_x in [0 ... 3] | |
for b_y in [0 ... 3] | |
for s_x in [0 ... 3] | |
for s_y in [0 ... 3] | |
key = "cell#{b_x}_#{b_y}_#{s_x}_#{s_y}" | |
@grid[b_x][b_y][s_x][s_y] = athis.state[key] | |
# @build_allow_grid() | |
athis | |
to_state: ()-> | |
state = {} | |
for b_x in [0 ... 3] | |
for b_y in [0 ... 3] | |
for s_x in [0 ... 3] | |
for s_y in [0 ... 3] | |
key = "cell#{b_x}_#{b_y}_#{s_x}_#{s_y}" | |
state[key] = @grid[b_x][b_y][s_x][s_y] | |
state | |
from_string: (str, separator)-> | |
line_list = str.split '\n' | |
if line_list.length != 9 | |
throw new Error "line_list.length != 9" | |
for line,y in line_list | |
pos_list = line.split separator | |
if pos_list.length != 9 | |
throw new Error "pos_list.length != 9" | |
for pos,x in pos_list | |
continue if pos.trim() == '' | |
b_x = x//3 | |
b_y = y//3 | |
s_x = x%%3 | |
s_y = y%%3 | |
@grid[b_x][b_y][s_x][s_y] = +pos | |
# @build_allow_grid() | |
return | |
# ################################################################################################### | |
# solver | |
# ################################################################################################### | |
solve : ()-> | |
start_ts = Date.now() | |
solver = new Logic.Solver() | |
for r9 in rule_9_list | |
[a1,a2,a3,a4,a5,a6,a7,a8,a9] = r9 | |
solver.require Logic.exactlyOne a1,a2,a3,a4,a5,a6,a7,a8,a9 | |
# esp_ts = Date.now() - start_ts | |
# console.log "BENCH2 #{esp_ts} ms" | |
# start_ts = Date.now() | |
r1_list = [] | |
# grid info export | |
for b_x in [0 ... 3] | |
for b_y in [0 ... 3] | |
for s_x in [0 ... 3] | |
for s_y in [0 ... 3] | |
val = @grid[b_x][b_y][s_x][s_y] | |
continue if !val? | |
r1_list.push "cell#{b_x}_#{b_y}_#{s_x}_#{s_y}_#{val}" | |
solver.require r1_list... | |
esp_ts = Date.now() - start_ts | |
console.log "BENCH3 #{esp_ts} ms" | |
start_ts = Date.now() | |
solution = solver.solve() | |
# esp_ts = Date.now() - start_ts | |
# console.log "#{esp_ts} ms" | |
# start_ts = Date.now() | |
var_list = solution.getTrueVars() | |
for cell in var_list | |
[_skip, b_x, b_y, s_x, s_y, val] = /^cell(\d)_(\d)_(\d)_(\d)_(\d)$/.exec cell | |
@grid[b_x][b_y][s_x][s_y] = +val | |
esp_ts = Date.now() - start_ts | |
console.log "#{esp_ts} ms" | |
return | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment