Data update
This commit is contained in:
parent
4d5544505c
commit
4924dd0264
3073 changed files with 55820 additions and 4408 deletions
40
Task/Sudoku/DuckDB/sudoku.duckdb
Normal file
40
Task/Sudoku/DuckDB/sudoku.duckdb
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
# An example
|
||||
.maxwidth 100
|
||||
set variable game='85 24 72 9 4 1 7 23 5 9 4 8 7 17 36 4 ';
|
||||
|
||||
create or replace function instr3(s,t,j) as (
|
||||
select case when n = 0 then 0
|
||||
else n + j - 1
|
||||
end
|
||||
from (select instr(substr(s,j), t) as n)
|
||||
);
|
||||
|
||||
with recursive
|
||||
symbols (d) as (select level::VARCHAR from range(1,10) t(level) )
|
||||
, board (i) as (select level from range(1,82) t(level) )
|
||||
, neighbors (i, j) as (
|
||||
select b1.i, b2.i
|
||||
from board b1 inner join board b2
|
||||
on b1.i != b2.i
|
||||
and (
|
||||
mod(b1.i - b2.i, 9) = 0
|
||||
or ceil(b1.i / 9) = ceil(b2.i / 9)
|
||||
or ceil(b1.i / 27) = ceil(b2.i / 27) and trunc(mod(b1.i - 1, 9) / 3) = trunc(mod(b2.i - 1, 9) / 3)
|
||||
)
|
||||
)
|
||||
, r (str, pos) as (
|
||||
select getvariable('game'), instr(getvariable('game'), ' ')
|
||||
|
||||
union all
|
||||
select substr(r.str, 1, r.pos - 1) || s.d || substr(r.str, r.pos + 1), instr3(r.str, ' ', r.pos + 1)
|
||||
from r inner join symbols s
|
||||
on r.pos > 0 and not exists (
|
||||
select *
|
||||
from neighbors n
|
||||
where r.pos = n.i and s.d = substr(r.str, n.j, 1)
|
||||
)
|
||||
)
|
||||
select str
|
||||
from r
|
||||
where pos = 0
|
||||
;
|
||||
104
Task/Sudoku/Zig/sudoku.zig
Normal file
104
Task/Sudoku/Zig/sudoku.zig
Normal file
|
|
@ -0,0 +1,104 @@
|
|||
const std = @import("std");
|
||||
|
||||
const Sudoku = [81]u8;
|
||||
|
||||
fn isValid(val: u8, x: usize, y: usize, sudokuAr: *Sudoku) bool {
|
||||
// Check row and column
|
||||
for (0..9) |i| {
|
||||
if (sudokuAr[y * 9 + i] == val or sudokuAr[i * 9 + x] == val) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Check 3x3 box
|
||||
const startX = (x / 3) * 3;
|
||||
const startY = (y / 3) * 3;
|
||||
|
||||
for (startY..startY + 3) |i| {
|
||||
for (startX..startX + 3) |j| {
|
||||
if (sudokuAr[i * 9 + j] == val) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
fn placeNumber(pos: usize, sudokuAr: *Sudoku) bool {
|
||||
var currentPos: usize = pos;
|
||||
|
||||
// Find next empty cell
|
||||
while (currentPos < 81 and sudokuAr[currentPos] != 0) {
|
||||
currentPos += 1;
|
||||
}
|
||||
|
||||
// If no empty cells, puzzle is solved
|
||||
if (currentPos >= 81) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const x = currentPos % 9;
|
||||
const y = currentPos / 9;
|
||||
|
||||
// Try placing digits 1-9
|
||||
for (1..10) |n| {
|
||||
if (isValid(@intCast(n), x, y, sudokuAr)) {
|
||||
sudokuAr[currentPos] = @intCast(n);
|
||||
|
||||
if (placeNumber(currentPos + 1, sudokuAr)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Backtrack
|
||||
sudokuAr[currentPos] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
fn prettyPrint(sudokuAr: Sudoku) void {
|
||||
const lineSep = "------+-------+------";
|
||||
std.debug.print("{s}\n", .{lineSep});
|
||||
|
||||
for (0..81) |i| {
|
||||
std.debug.print("{d} ", .{sudokuAr[i]});
|
||||
|
||||
if ((i + 1) % 3 == 0 and (i + 1) % 9 != 0) {
|
||||
std.debug.print("| ", .{});
|
||||
}
|
||||
|
||||
if ((i + 1) % 9 == 0) {
|
||||
std.debug.print("\n", .{});
|
||||
}
|
||||
|
||||
if ((i + 1) % 27 == 0) {
|
||||
std.debug.print("{s}\n", .{lineSep});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn solve(sudokuAr: *Sudoku) bool {
|
||||
return placeNumber(0, sudokuAr);
|
||||
}
|
||||
|
||||
pub fn main() !void {
|
||||
var sudokuAr: Sudoku = [_]u8{
|
||||
8, 5, 0, 0, 0, 2, 4, 0, 0,
|
||||
7, 2, 0, 0, 0, 0, 0, 0, 9,
|
||||
0, 0, 4, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 1, 0, 7, 0, 0, 2,
|
||||
3, 0, 5, 0, 0, 0, 9, 0, 0,
|
||||
0, 4, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 8, 0, 0, 7, 0,
|
||||
0, 1, 7, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 3, 6, 0, 4, 0
|
||||
};
|
||||
|
||||
if (solve(&sudokuAr)) {
|
||||
prettyPrint(sudokuAr);
|
||||
} else {
|
||||
std.debug.print("Unsolvable\n", .{});
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue