Skip to content

Instantly share code, notes, and snippets.

@dsasse07
dsasse07 / sudoku-full.rb
Last active February 18, 2021 04:30
OO Sudoku Board generator & solver - Basis for my sudoku_wizard gem
class Sudoku
attr_accessor :starting_board, :solution, :removed_values, :difficulty
BLANK_BOARD = [
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
@dsasse07
dsasse07 / sudoku-col-condition.js
Last active February 18, 2021 00:34
Sudoku safe placement in column check
// puzzleArray is the game board being solved. A 9x9 matrix
// emptyCell = {rowIndex: INT , colIndex: INT } INT = coordinates of currently empty cell
// num = integer value 1-9 being tested
const colSafe = (puzzleArray, emptyCell, num) => {
return !puzzleArray.some(row => row[ emptyCell.colIndex ] == num )
}
@dsasse07
dsasse07 / sudoku-next-empty.js
Last active February 17, 2021 18:03
Find next empty cell Sudoku
const nextEmptyCell = puzzleArray => {
const emptyCell = {rowIndex: "", colIndex: ""}
puzzleArray.forEach( (row, rowIndex) => {
// If this key has already been assigned, skip iteration
if (emptyCell.colIndex !== "" ) return
// find first zero-element
let firstZero = row.find( col => col === 0)
@dsasse07
dsasse07 / sudoku-fill-puzzle.js
Last active February 17, 2021 17:41
Sudoku recursive solver function & shuffler
// startingBoard is a 9x9 matrix of zeros
const numArray = [1, 2, 3, 4, 5, 6, 7, 8, 9]
const shuffle = array => {
let newArray = [...array]
for ( let i = newArray.length - 1; i > 0; i-- ) {
const j = Math.floor( Math.random() * ( i + 1 ) );
[ newArray[ i ], newArray[ j ] ] = [ newArray[ j ], newArray[ i ] ];
}
@dsasse07
dsasse07 / sudoku-safe-helper.js
Last active February 17, 2021 17:32
Helper function for checking number placement
const safeToPlace = ( puzzleArray, emptyCell, num ) => {
return rowSafe(puzzleArray, emptyCell, num) &&
colSafe(puzzleArray, emptyCell, num) &&
boxSafe(puzzleArray, emptyCell, num)
}
@dsasse07
dsasse07 / sudoku-row-condition.js
Last active February 17, 2021 17:31
Sudoku safe placement row condition in JS
// puzzleArray is the game board being solved. A 9x9 matrix
// emptyCell = {rowIndex: INT , colIndex: INT } INT = coordinates of currently empty cell
// num = integer value 1-9 being tested
const rowSafe = (puzzleArray, emptyCell, num) => {
// -1 is return value of .find() if value not found
return puzzleArray[ emptyCell.rowIndex ].indexOf(num) == -1
}
@dsasse07
dsasse07 / sudoku-initialize-functions.js
Last active February 17, 2021 13:26
Sudoku initialize functions
const newSolvedBoard = _ => {
startTime = new Date
// Create an unaffiliated clone of a fresh board
const newBoard = BLANK_BOARD.map(row => row.slice() )
// Populate the board using backtracking algorithm
fillPuzzle(newBoard)
return newBoard
}
@dsasse07
dsasse07 / sudoku-box-condition.js
Last active February 16, 2021 05:08
Sudoku box region safe condition
// puzzleArray is the game board being solved. A 9x9 matrix
// emptyCell = {rowIndex: INT , colIndex: INT } INT = coordinates of currently empty cell
// num = integer value 1-9 being tested
const boxSafe = (puzzleArray, emptyCell, num) => {
// Define top left corner of box region for empty cell
boxStartRow = emptyCell.rowIndex - (emptyCell.rowIndex % 3)
boxStartCol = emptyCell.colIndex - (emptyCell.colIndex % 3)
let safe = true
@dsasse07
dsasse07 / center_align.rb
Last active January 10, 2021 23:20
Creating a monkey patch to center align a string
require 'tty-screen'
class String
def center_align
screen_width = TTY::Screen.width
screen_center = screen_width / 2
string_length = self.length
string_center = string_length/2
offset = screen_center - string_center
"#{sprintf("%#{offset}s" % self)}"
@dsasse07
dsasse07 / subscription_tracker_environment.rb
Created January 10, 2021 22:28
Environment file for SubscriptionTracker loading the module files in heirarchy order before loading the model files
require 'bundler'
Bundler.require
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: 'db/development.db')
ActiveRecord::Base.logger = nil
require_relative '../app/tools/funstuff.rb' #=> Begin loading files from the bottom of the heirarchy
require_relative '../app/tools/cli_controls.rb' #=> Step 2 on the heirarchy