Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
BAXYCode committed Apr 11, 2023
1 parent dbf09d5 commit a79db96
Show file tree
Hide file tree
Showing 9 changed files with 325 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "dlx"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
70 changes: 70 additions & 0 deletions src/LinkedList.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
use std::ops;



#[derive(Debug,PartialEq)]
struct LinkedList{
links: Vec<Link>
}


struct Link{
prev: Cell,
next: Cell}

impl ops::Index<Cell> for LinkedList{

type Output = Link;
fn index(&self, a: Cell) -> &Link{
&self.links[a.0]}


}impl ops::IndexMut<Cell> for LinkedList{
fn index_mut(&self, a:Cell) -> &mut Link{
&mut self.links[a.0]}}


impl LinkedList {

fn new_with_cap(cap: uzise) -> LinkedList{
LinkedList{links: Vec::with_capacity(cap), covered:false,num:Num::None}
}

fn new_cell(&mut self) -> Cell{
let cell = Cell(self.links.len());
self.links.push(Link{prev:cell, next:cell});
cell}

fn insert(&mut self, a:Cell,b:Cell){

let c = self[a].next;

self[a].next = b;
self[b].next = c;
self[c].prev = b;
self[b].prev = a; }


fn remove(&mut self, a:Cell){

let a = self[b].prev;
let c = self[b].next;

self[c].prev = self[b].prev;
self[a].next = self[b].next;}

fn add_back(&mut self, b:Cell){

let a = self[b].prev;
let c = self[b].next;

self[c].prev = b;
self[a].next = b;}

fn cursor(&self, c: Cell) -> Cursor{
Cursor {c, c}}
}

fn main() {

}
Empty file added src/cells.rs
Empty file.
27 changes: 27 additions & 0 deletions src/cursor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
mod LinkedList;
mod cells;

struct Cursor{
head: Cell,
current: Cell}
impl Cursor {

//Takes in a reference to a list and uses the cursor's internals to
//navigate said list. If the cursor's new curr node is the same as
//the node it started with, None is returned.
fn next(&self, &list: LinkedList) -> Option<Cell>{

self.current = list[self.current].next;

if assert_eq!(self.current,self.head ){
return None;}
Some(self.current);
}
fn prev(&self, &list:LinkedList) -> Option<Cell>{

self.current = list[self.current].prev;

if assert_eq!(self.current, self.head){
return None;}
Some(self.current);}
}
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
unimplemented!();
116 changes: 116 additions & 0 deletions src/matrix.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
mod cells;
mod cursor;
mod LinkedList;


struct Matrix{
horizontal: LinkedList,
vertical: LinkedList,

access: Vec<Cell>,

sizes: Vec<usize>,
}

impl Matrix{

pub fn new(n: usize, rows:usize,cols:usize) -> Matrix{

let mut matrix = Matrix{
horizontal : Vec::with_capacity(cols+1),
vertical : Vec::with_capacity(rows+1),
size: Vec::new(),
access: Vec::with_capacity(cols+1),
}
for _ in 0..cols{
matrix.add_column();
}
matrix
}

fn allocate_cell(&mut self, cell: Cell) -> Cell{

self.access.push(cell);
let x_cell = self.horizontal.new_cell();
let y_cell = self.vertical.new_cell();
assert_eq!(x_cell,y_cell );
x_cell
}
fn allocate_column(&mut self) -> Cell{

let cell = self.allocate_cell(CERO);
self.access[cell] = cell;
self.size.push(0);
cell
}

fn add_column(&mut self) -> Cell {

let cell = self.allocate_column();

// add column in last position
self.horizontal.insert(self.horizontal[CERO].prev,cell);

cell
}
pub fn add_row(&mut self, row: Vec<u32>) {


let mut col = CERO;

let mut prev = None;
for val in row{
col = self.horizontal[col].next;
match val{
Some(0) => _,
Some(1) =>{
self.size[col] += 1;
let new_cell = self.allocate_cell();
//fetch column and add new cell as cols new previous cell
self.vertical.insert(self.vertical[col].prev,new_cell);
if let Some(prev) =prev{
self.horizontal.insert(prev,new_cell);
}
prev = Some(new_cell);}
}
}
}
fn cover(&mut self, cell:Cell){
//remove acces cell front x axis list
self.horizontal.remove(cell);
//get y axis cursor to iterate on rows to cover
let mut cur = self.y.cursor(cell);
//here we only cover the rows but we leave the covered columns nodes
//for us to cover later outside of this function
while let Some(c_axis_cell) = cur.next(&self.vertical){
let mut curr = self.horizontal.cursor(c_axis_cell);
while let Some(r_axis_cell) = curr.next(&self.horizontal){

self.vertical.remove(r_axis_cell);
self.size[self.acces[r_axis_cell]] -= 1;

}

}
}


fn uncover(&mut self, cell:Cell){

let mut cur = self.vertical.cursor(cell);
while let Some(c_axis_cell) = cur.prev(&self.vertical){

let mut r_axis_cell = self.horizontal.cursor(c_axis_cell);
while let Some(current_cell) = r_axis_cell.prev(&self.horizontal){

self.size[self.access[current_cell]] += 1;
self.vertical.add_back(current_cell);

}

}
self.horizontal.add_back(cell);

}

}
95 changes: 95 additions & 0 deletions src/sudoku.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#![allow(unused)]
#![feature(int_roundings)]

use std::itertools::*;

struct Sudoku{
sudoku:Vector<usize>,
dimension:usize,}

impl Sudoku{

fn sudoku_to_sparse() -> Vec<Vec<usize>>{
let mut sparse = Vec::new();

for (ind, val) in enumerate(self.sudoku){

if val == 0{

let mut temp = self.build_nine_rows(ind);
temp.iter.map(|item|sparse.push(item));

}else{

let temp = self.build_one_row(ind,val);
sparse.push(temp);

}

}
sparse}

fn get_index(dim:usize, val:usize, constraint_ind:usize) -> usize{

dim*(constraint_ind-1)+val

}

fn build_one_row(ind:usize,val: usize) -> Vecusize{

let dimension =
self.dimension*self.dimension*self.dimension*self.dimension;
let n = self.dimension*self.dimension;
let mut completed_row = Vec::new();

let mut col = ind%(self.dimension*self.dimension);
if col == 0{
col = 9;}

let row = ind.div_ceil(self.dimension*self.dimension);
let box = (row-(row%self.dimension))+row.div_ceil(self.dimension);

let row_index = self.get_index(n,val,row) ;
let col_index = self.get_index(n,val,col);
let box_index = self.get_index(n,val,box);
let digit_index = get_index(n,val,ind);

let mut row_vec = Vec::new();
let mut col_vec = Vec::new();
let mut box_vec = Vec::new();
let mut digit_vec = Vec::new();

for i in 0..dimension{

if i == col_index{
col_vec.push(1usize);
}else{col_vec.push(0usize);}
if i == row_index{
row_vec.push(1usize);}
else{row_vec.push(0usize);}
if i == box_index{box_vec.push(1usize);}
else{box_vec.push(0usize);}
if i == digit_index{digit_vec.push(1usize);}
else{digit_vec.push(0usize);}

}
completed_row.push(digit_vec);
completed_row.push(row_vec);
completed_row.push(col_vec);
completed_row.push(box_vec);
completed_row
}

fn build_nine_rows(dim: usize){

let mut result = Vec::new();

for i in 1..=dim*dim{
let temp = build_one_row(dim,i);
result.push(temp);

}
result
}

}

0 comments on commit a79db96

Please sign in to comment.