Skip to content

Instantly share code, notes, and snippets.

View petertseng's full-sized avatar

Peter Tseng petertseng

View GitHub Profile
@petertseng
petertseng / 123
Last active December 26, 2016 23:49
day11inputs
The first floor contains a 1 generator, a 2 generator, a 3 generator, a 4 generator, a 5 generator, a 6 generator, a 7 generator, a 8 generator, a 9 generator, a 10 generator, a 11 generator, a 1-compatible microchip, a 2-compatible microchip, a 3-compatible microchip, a 4-compatible microchip, a 5-compatible microchip, a 6-compatible microchip, a 7-compatible microchip, a 8-compatible microchip, a 9-compatible microchip, a 10-compatible microchip, and a 11-compatible microchip.
The second floor contains nothing relevant.
The third floor contains nothing relevant.
The fourth floor contains nothing relevant.
@petertseng
petertseng / md515.cr
Last active December 27, 2016 08:19
day 15 with extra hash powers
require "crypto/md5"
hash = "0" * 32
zeroes = Array.new(32, 1)
ROWS = 8
almost = 0
hashes = Array.new(ROWS, "")
hashes[0] = hash
@petertseng
petertseng / regex_golf.rb
Created December 19, 2016 23:28
Regex Golf
# http://norvig.com/ipython/xkcd1313.ipynb
# http://nbviewer.jupyter.org/url/norvig.com/ipython/xkcd1313.ipynb
#
# http://norvig.com/ipython/xkcd1313-part2.ipynb
# http://nbviewer.jupyter.org/url/norvig.com/ipython/xkcd1313-part2.ipynb
#
# Only implemented the 'covers' optimisation in part 2
# no branch and bound.
# no repetition characters or pairs
@petertseng
petertseng / astar.rb
Last active December 27, 2016 08:14
A* search, maybe useful someday
require 'set'
def heuristic(start, goal)
{
start: 5, # Not given in the diagram but probably not important.
a: 4,
b: 2,
c: 4,
d: 4.5,
e: 2,
@petertseng
petertseng / vttoffset.rb
Created January 3, 2017 01:12
VTT offset
if ARGV.empty?
puts "usage: #{$PROGRAM_NAME} offset [file] (or stdin)"
exit 1
end
offset = ARGV.shift.to_i
ARGF.each_line { |l|
unless l.include?('-->')
puts l
@petertseng
petertseng / exercism-teams.rb
Last active May 10, 2017 06:48
Exercism teams
require 'json'
require 'set'
# Create a personal access token at https://github.com/settings/tokens
# It needs only one scope: read:org ("Read org and team membership").
TOKEN_FILE = 'secrets/token.txt'.freeze
SLEEP_TIME = 2
global_name = 'track-maintainers'.freeze
@petertseng
petertseng / submit.rb
Last active November 3, 2019 03:36
Exercism autosubmit
#!/usr/bin/env ruby
if ARGV.empty?
puts "usage: #{$PROGRAM_NAME} dir"
Kernel.exit(1)
end
track = File.basename(File.dirname(File.realpath(ARGV[0])))
def glob(*components, except: [])
@petertseng
petertseng / minijson.rb
Created March 17, 2017 04:56
mini JSON
class ParseError < StandardError; end
def object(s, i)
o = {}
state = :key
key = nil
j = i + 1
while (c = s[j])
case state
require 'json'
require 'time'
# Create a personal access token at https://github.com/settings/tokens
# It needs NO scopes.
# In fact, you could just not use a token, but then you get rate-limited to 60 an hour instead of 5000 an hour.
TOKEN_FILE = 'secrets/token.txt'.freeze
SLEEP_TIME = 2
CACHE = 'cache'.freeze
@petertseng
petertseng / insert_signs.rb
Created November 14, 2017 04:39
add up to 100
def ops(nums, result, sign = 1)
case nums.size
when 0, 1
result == (nums[0] || 0) * sign ? [[nums[0]].compact] : []
else
n1, n2 = nums.first(2)
plus = ops(nums.drop(1), result - n1 * sign, 1).map { |r| [n1, :+] + r }
minus = ops(nums.drop(1), result - n1 * sign, -1).map { |r| [n1, :-] + r }
concat = ops([n1 * 10 + n2] + nums.drop(2), result, sign)
plus + minus + concat