Skip to content

Instantly share code, notes, and snippets.

View petertseng's full-sized avatar

Peter Tseng petertseng

View GitHub Profile
@petertseng
petertseng / lightsout.rb
Last active September 8, 2020 23:04
Lights Out
# http://www.logicgamesonline.com/lightsout/daily.php
def popcount(x)
b = 0
while x > 0
x &= x - 1
b += 1
end
b
end
# https://www.chiark.greenend.org.uk/~sgtatham/puzzles/js/signpost.html
# This should work on every single puzzle generated,
# because this solver uses the same reasoning as that used by the generator to check for solvability:
# check for cells with only a single possible successor or a single possible predecessor.
#
# There are other techniques that can be used:
# * Search with distance > 1, for example check which cell will eventually allow 24 to connect to 27.
# * Naked groups and hidden groups:
# * If N preds have N succs as their only possible succs, discard all other possible preds from those succs
# * If N succs have N preds as their only possible preds, discard all other possible succs from those preds
@petertseng
petertseng / grid.rb
Last active September 7, 2020 02:43
Towers
# https://www.chiark.greenend.org.uk/~sgtatham/puzzles/js/towers.html
# Should be able to do Hard,
# which is listed in latin.c as diff_set_0,
# handled by checking all permutations for all rows + columns,
# and can do Extreme,
# which is listed in latin.c as both diff_set_1 and diff_forcing,
# which are implemented as xwing and forcing_chain
#
# https://www.janko.at/Raetsel/Wolkenkratzer/index.htm
# Have not yet determined whether any puzzle on janko.at actually has diagonal labels,
approach:
it takes 0 jobs to print 0.
for each attendee count from 1 up to n:
for each square not larger than attendee count:
we can do it in 1 + jobs[attendee_count - square]
take the smallest of the above possibilities
time:
proportional to n * sqrt(n)
@petertseng
petertseng / deduce.rb
Last active August 10, 2019 01:14
letter jam help
files, hints = ARGV.partition { |x| File.file?(x) }
hint = hints.join
dict_words = {}
hints = (files.empty? ? File.open('words_alpha.txt', ?r) : ARGF).each_line.map { |word|
dict_words[word.chomp.downcase] = true
}
non_letters = hint.chars.reject { |c| (?a..?z).cover?(c) }.uniq
@petertseng
petertseng / lc4.rb
Last active October 26, 2018 05:17
lc4 and ls47
VERBOSE = ARGV.delete('-v')
def lc4(key)
keyed_cipher('#_23456789abcdefghijklmnopqrstuvwxyz', key, marker_moves: true)
end
def ls47(key)
keyed_cipher("_abcdefghijklmnopqrstuvwxyz.0123456789,-+*/:?!'()", key, marker_moves: false)
end
require 'net/http'
require 'json'
require 'set'
require 'yaml'
REPEATS = true
LEVEL = 6
test_all = ARGV.delete('-a')
@petertseng
petertseng / mastermind.cr
Last active August 13, 2020 08:17
Mastermind
alias Elt = Int32
alias Count = Int32
SPOTS = 4
COLOURS = 6
REPEATS = true
OPT = true
INTERACTIVE = ARGV.includes?("-i") || ARGV.includes?("--interactive")
@petertseng
petertseng / input.txt
Last active January 3, 2018 02:54
day 21 outputs
1000
5668137592934713102441324678542073232628021211352952834657990662080563989156194797022212756047286793446774589470677745088211552869413620590162331251809444529147879775001431595233001085455497658846273053988435137758831824174489016555551028952432382770343365256475301053130655210572847728724711590986746488551898866722597
10000
302669529812541170422608884660770639502536277197232986875137298025407684442895444028081551314183007929910187728205214188180336420042946217586843869909595752045864386118547444229863779633877539380779777189206033374459242954935816500440170965196410365414059489031105815025057687540084623895989944745185216200007560377828236971085502033017748749836090693164795523436819777444319230491066901927876983503441688873720350325322027957147468352440461421415495530179964036198655325256234685976439258185029842576578151300809548942649999578088645184924924030509833459994139919002177456748428928269786399145831490634691218139368719684999928952102474893451058277888843495254783378609260731391089662750735222
0: 2
1: 12
3: 4
5: 8
7: 10
9: 8
11: 18
13: 8
15: 8
17: 33