Skip to content

Instantly share code, notes, and snippets.

@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-hole-puncher.js
Last active February 24, 2021 17:04
Punch holes in filled sudoku board
const pokeHoles = (startingBoard, holes) => {
const removedVals = []
const val = shuffle( range(0,80) )
while (removedVals.length < holes) {
const nextVal = val.pop()
if (nextVal === undefined) throw new Error ("Impossible Game")
const randomRowIndex = Math.floor(nextVal / 9) // Integer 0-8 for row index
const randomColIndex = nextVal % 9
@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-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 / 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-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 / 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
@dsasse07
dsasse07 / module_inheritance.rb
Last active January 10, 2021 22:08
Modules that are inherited can be passed along to a new parent Module
module Module_A
def greeting_a
puts "Hello from Module A"
end
end
module Module_B
include Module_A