Skip to content

Instantly share code, notes, and snippets.

@tuzz
tuzz / miracle-sudoku-tweet-solver.snt
Created Aug 13, 2020
A quick Sentient program to solve a sudoku posted in a tweet
View miracle-sudoku-tweet-solver.snt
# A quick Sentient program to solve the sudoku posted in this tweet:
# https://twitter.com/crypticcracking/status/1293897459912056833
function main () {
array9<array9<int5>> sudoku;
sudoku.each(function (row) {
invariant row.uniq?;
invariant row.all?(function (n) {
View Cargo.toml
[package]
name = "foo"
version = "0.1.0"
edition = "2018"
[dependencies]
legion = "0.2.4"
@tuzz
tuzz / gaussian
Last active May 3, 2020
Generate the weights/offsets for an efficient two-pass Gaussian blur filter of different sizes.
View gaussian
#!/usr/bin/env ruby
# Generate the weights/ offsets for an efficient two-pass Gaussian blur filter of different sizes.
# Based on: http://rastergrid.com/blog/2010/09/efficient-gaussian-blur-with-linear-sampling/
#
# Usage: ./gaussian [epsilon]
require "bigdecimal/util"
EPSILON = Float(ARGV[0] || 0.05)
@tuzz
tuzz / web_sys_event_handler.rs
Created Apr 19, 2020
A convenience wrapper for handling events in Rust using web_sys
View web_sys_event_handler.rs
// The EventHandler::new method takes three arguments:
//
// 1) The Rust function to call to handle the event
// 2) The registration function
// 3) The deregistration function
//
// The deregistration function is called when the EventHandler is dropped.
// This is similar to React's useEffect cleanup pattern.
pub struct EventHandler {
View parker_square.snt
int8 m, n, p, q;
m2 = m.square;
n2 = n.square;
p2 = p.square;
q2 = q.square;
# ceil(log2(sqrt((2 ** (bits - 1)) ** 2 ** 2 * 3)) + 1)
int16 inner, outer1, outer2;
int2 i;
@tuzz
tuzz / magic_square_7_of_8.snt
Last active Jan 8, 2020
Search for magic squares of square numbers that have one mistake, either a row, column or diagonal
View magic_square_7_of_8.snt
#!/usr/bin/env sentient -c -o -r -n 0 -m lingeling magic_square_7_of_8.snt
# Based on: https://twitter.com/standupmaths/status/1214921447644250113
# Written in Sentient: https://sentient-lang.org/
function main() {
int12 a, b, c, d, e, f, g, h, i;
# Needs to be: ceil(log2((2^(bits - 1))^2 * 3)) + 1
int25 magicSum;
@tuzz
tuzz / bandits.rb
Created Dec 15, 2019
Playing with 'bandits' from reinforcement learning
View bandits.rb
NUMBER_OF_BANDITS = 4
EPSILON = 0.001
VALUES_AND_OCCURENCES = NUMBER_OF_BANDITS.times.map { [0, 0] }
def greedy_action
VALUES_AND_OCCURENCES.map.with_index { |(v, o), i| [v, o, i] }.max_by(&:first).last
end
def random_action
@tuzz
tuzz / digit-reversal.snt
Last active Oct 27, 2019
A Sentient program to find solutions to questions posed in 'Digit Reversal Without Apology'
View digit-reversal.snt
#!/usr/bin/env sentient -c -o -r -n 0 -m lingeling digit-reversal.snt
# https://arxiv.org/pdf/math/0511366.pdf
# This program explores 'Open question one'
array5<int9> digits;
base = 198;
max = 197;
@tuzz
tuzz / superpermutation-spiral.html
Created Mar 6, 2019
A very quick experiment to draw superpermutations as colored spirals moving out from the center.
View superpermutation-spiral.html
<center>
<canvas id="canvas" width="1000" height="1000"></canvas>
</center>
<script>
var canvas = document.getElementById("canvas");
var context = canvas.getContext("2d");
var done = false;
function circle_around(callback) {
View simplesupersat.rb
# This is a greatly simplified version of https://github.com/tuzz/supersat
# It reduces the superpermutation problem to boolean satisfiability.
#
# To generate a SAT problem for N=3, LENGTH=9:
# ruby simplesupersat.rb 3 9
#
# And to run a SAT solver on it:
# lingeling 3-9.dimacs
N = Integer(ARGV[0])
You can’t perform that action at this time.