Skip to content

Instantly share code, notes, and snippets.

@ggPeti
ggPeti / keybase.md
Last active February 20, 2018 15:16

Keybase proof

I hereby claim:

  • I am ggpeti on github.
  • I am ggpeti (https://keybase.io/ggpeti) on keybase.
  • I have a public key ASDfM5n7IbSbHEjogUBecO6zn9XUa7BNebgxgnq5NvynvQo

To claim this, I am signing this object:

#!/usr/bin/env ruby
# Wolfram cellular automaton simulator
# Usage: ./rule.rb <rulenr> [<generations>|100]
rule = 7.downto(0).map { |x| x.to_s(2).rjust(3, ?0).chars.map(&:to_i) }.zip(ARGV.shift.to_i.to_s(2).rjust(8, ?0).chars.map(&:to_i)).to_h
steps = ARGV.shift&.to_i || 100
row = [0] * steps + [1] + [0] * steps
steps.times {
puts(row.map { |c| c == 0 ? ' ' : "\u2588" }.join)
@ggPeti
ggPeti / asdf
Created February 1, 2015 12:00
My KOHCTPYKTOP solutions
eNrtmVFygzAMRLGWn5yhV+h/z9L7X6SpMbXAkmpCQmi69jDM5GnBA/FawuP7+Hb5
TJePNA63dAoppJBCCimk8EChbBHKT09PEsptwtQrTKoP2+74cv+cnM6J3zIFYB77
aW7jcP1hGk896fvXKOjIJkRQm4cntch8tmK+aW+EMGBXwBVkFgQUtX+PAwLkv/Py
Hn3euIami4hmwopDy5T38RQgegArniNEX6IEzKNGNY2j/M702S4jJiUlJcUDKU7/
NGiVpKSkJ6VxPkyrJCUlPVXeCFmV6EstQppLaPHzRr+CnwvwU+SctEpSUtIOqwwo
YoqgxC5fSlmAk5KSvkghHGSVIZWYrtNGnVX+uQJ8/47S9Cyg1x5zVYK7KolPf93d
IyUl7aIIzNCnEtMmKW09x9oB18V/vcAD97jvYpX2sZ9aeb/1IhCuSliuPswNSEnv
WoAbk3QrBTZqFylSneHHOFLbvgCEdsjC
@ggPeti
ggPeti / Level 3 - AND, OR
Last active August 29, 2015 14:14
My KOHCTPYKTOP solutions
eNrtmVFygzAMRLGWn5yhV+h/z9L7X6SpMbXAkmpCQmi69jDM5GnBA/FawuP7+Hb5
TJePNA63dAoppJBCCimk8EChbBHKT09PEsptwtQrTKoP2+74cv+cnM6J3zIFYB77
aW7jcP1hGk896fvXKOjIJkRQm4cntch8tmK+aW+EMGBXwBVkFgQUtX+PAwLkv/Py
Hn3euIami4hmwopDy5T38RQgegArniNEX6IEzKNGNY2j/M702S4jJiUlJcUDKU7/
NGiVpKSkJ6VxPkyrJCUlPVXeCFmV6EstQppLaPHzRr+CnwvwU+SctEpSUtIOqwwo
YoqgxC5fSlmAk5KSvkghHGSVIZWYrtNGnVX+uQJ8/47S9Cyg1x5zVYK7KolPf93d
IyUl7aIIzNCnEtMmKW09x9oB18V/vcAD97jvYpX2sZ9aeb/1IhCuSliuPswNSEnv
WoAbk3QrBTZqFylSneHHOFLbvgCEdsjC
@ggPeti
ggPeti / crusher.scala
Created December 19, 2014 12:38
IDEA highlight bug p-o-c
object unapplier { def unapply(x: Int) = Some((x, x)) }
val tupleTaker = (_: (Int, Int)) => ()
1 match {
case unapplier(tuple) => tupleTaker(tuple)
}
@ggPeti
ggPeti / ext.rb
Created September 3, 2014 08:42
Arrow-like proc handling in Ruby
class Object
def itself
self
end
%i[> >> * &].each { |method| define_method(method) { |*args| to_proc.send method, *args } }
end
class String
def balanced?
gsub! /\(\)|\[\]|\{\}/, '' while $~
empty?
end
end
@ggPeti
ggPeti / array_hopper.rb
Created November 17, 2013 22:09
A linear time solution for the ArrayHopper problem.
numbers = File.read(ARGV.first).split.map &:to_i
best_path = [0]
loop do
current_pos = best_path.last
max_hop = numbers[current_pos]
candidates = (1..max_hop).map { |hop| current_pos + hop }
if candidates.empty?
best_path = ["failure"]
@ggPeti
ggPeti / spiral.rb
Last active December 26, 2015 17:39
Procedural Ruby solution for the Spiral task
def spiral(h, w, r, c)
spiral = []
step_counts = (1..Float::INFINITY).lazy.flat_map { |n| [n, n] }
directions = [:up, :left, :down, :right].cycle
while spiral.length < h * w
spiral << (r - 1) * w + c if r.between?(1, h) && c.between?(1, w)
if (current_step_count ||= 0).zero?
@ggPeti
ggPeti / hash.rb
Created June 17, 2013 09:22
Hash extension with key and value mapper methods.
class Hash
def map_keys
Hash[self.map { |key, value| [yield(key), value] }]
end
def map_keys!
keys.each { |key| self[yield(key)] = delete key }