Skip to content

Instantly share code, notes, and snippets.

@andmcgregor
andmcgregor / gist:5453318
Created April 24, 2013 16:05
First attempt at sudoku solver.
CELL_POSSIBILITIES = [1, 2, 3, 4, 5, 6, 7, 8, 9]
def print_sudoku(example)
example.each do |line|
line.each do |cell|
print cell.to_s + " "
end
puts
end
end
DICE = [['A','A','E','E','G','N'],
['E','L','R','T','T','Y'],
['A','O','O','T','T','W'],
['A','B','B','J','O','O'],
['E','H','R','T','V','W'],
['C','I','M','O','T','U'],
['D','I','S','T','T','Y'],
['E','I','O','S','S','T'],
['D','E','L','R','V','Y'],
['A','C','H','O','P','S'],
class Sudoku
def initialize(sudoku_string)
@sudoku = sudoku_string.split("").map {|elem| elem.to_i }
9.times { @sudoku << @sudoku.shift(9) }
insert_possible_values
end
def solve!
100.times do
@sudoku.each_with_index do |row, row_i|
DICE = [['A','A','E','E','G','N'],
['E','L','R','T','T','Y'],
['A','O','O','T','T','W'],
['A','B','B','J','O','O'],
['E','H','R','T','V','W'],
['C','I','M','O','T','U'],
['D','I','S','T','T','Y'],
['E','I','O','S','S','T'],
['D','E','L','R','V','Y'],
['A','C','H','O','P','S'],
@andmcgregor
andmcgregor / gist:5737633
Created June 9, 2013 04:24
Brute force boggle
DICE = [['A','A','E','E','G','N'],
['E','L','R','T','T','Y'],
['A','O','O','T','T','W'],
['A','B','B','J','O','O'],
['E','H','R','T','V','W'],
['C','I','M','O','T','U'],
['D','I','S','T','T','Y'],
['E','I','O','S','S','T'],
['D','E','L','R','V','Y'],
['A','C','H','O','P','S'],
puts "\033[92m" + "Green = First letter" + "\033[0m"
puts "\033[91m" + "Red = Second letter" + "\033[0m"
puts "\033[93m" + "Yellow = Third letter" + "\033[0m"
puts "\033[94m" + "Blue = Fourth letter" + "\033[0m" if !trace[3].nil?
puts "\033[95m" + "Pink = Fifth letter" + "\033[0m" if !trace[4].nil?
def green
"\033[92m" + yield + "\033[0m"
end
@andmcgregor
andmcgregor / brute_force_sudoku.rb
Created June 10, 2013 05:24
Brute force sudoku (work in progress). Need to: - make the code ALOT more efficient. I have a few ideas how to do this. - improve found algorithm to actually check that the board is solved (rather than just adding up the numbers and seeing if they equal 405). - pause the search once a solution is found.
class SudokuCell
attr_accessor :index, :value, :row, :column, :box
def initialize(initial_value, index)
@value = initial_value != 0 ? initial_value : 0
@index = index.to_i
@row = (0..81).to_a[((index/9)*9..(index/9)*9+8)]
@column, at_index, incrementor = [], index, +9
until @column.count == 9 do
if at_index < 0
incrementor = +9
# encoding: utf-8
DELETE FROM voters
WHERE homeowner = 1
AND employed = 1
AND children_count = 0
AND party_duration < 3
AND (SELECT voters.id
FROM voters
INNER JOIN votes
ON voters.id = votes.voter_id
INNER JOIN congress_members