Skip to content

Instantly share code, notes, and snippets.

View nudded's full-sized avatar

Toon Willems nudded

View GitHub Profile
#!/usr/bin/env ruby
require "net/http"
require "uri"
require "json"
require "base64"
class Imgur
API_KEY = "15a6efd29935f62ea87579a8325d945a"
def raise_error(error_name, superclass = Exception)
raise self.class.const_get error_name
rescue NameError
self.class.const_set error_name, Class.new(superclass)
retry
end
data Protocol
= Hybi00
| Hybi01
sendMessage :: Protocol -> Message -> IO ()
sendMessage HybiOO (Text text) = putStrLn $ "[Hybi00] " ++ text
sendMessage HybiOO (Binary _) = error "LOL"
sendMessage Hybi01 (Text text) = ..
@nudded
nudded / gist:2864553
Created June 3, 2012 18:40
W3C validator for css and html
require 'rest_client'
require 'nori'
Nori.configure do |config|
config.strip_namespaces = true
config.convert_tags_to { |tag| tag.snakecase.to_sym }
end
module W3C
{-# LANGUAGE GADTs, FlexibleInstances, MultiParamTypeClasses #-}
import Control.Monad.State.Class
import Control.Monad.Trans.Class
data StateD s m a where
Get :: StateD s m s
Put :: s -> StateD s m ()
Return :: a -> StateD s m a
Bind :: StateD s m a -> (a -> StateD s m b) -> StateD s m b
Lift :: m a -> StateD s m a
{-# LANGUAGE GADTs, FlexibleInstances, MultiParamTypeClasses #-}
import Control.Monad.State.Class
import Control.Monad.Trans.Class
data StateD s m a where
Get :: StateD s m s
Put :: s -> StateD s m ()
Return :: a -> StateD s m a
Bind :: StateD s m a -> (a -> StateD s m b) -> StateD s m b
Lift :: m a -> StateD s m a
{:ABS_URI=>/\A\s*
([a-zA-Z][\-+.a-zA-Z\d]*): (?# 1: scheme)
(?:
((?:[\-_.!~*'()a-zA-Z\d;?:@&=+$,]|%[a-fA-F\d]{2})(?:[\-_.!~*'()a-zA-Z\d;\/?:@&=+$,\[\]]|%[a-fA-F\d]{2})*) (?# 2: opaque)
|
(?:(?:
\/\/(?:
(?:(?:((?:[\-_.!~*'()a-zA-Z\d;:&=+$,]|%[a-fA-F\d]{2})*)@)? (?# 3: userinfo)
(?:((?:(?:[a-zA-Z0-9\-.]|%\h\h)+|\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}|\[(?:(?:[a-fA-F\d]{1,4}:)*(?:[a-fA-F\d]{1,4}|\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})|(?:(?:[a-fA-F\d]{1,4}:)*[a-fA-F\d]{1,4})?::(?:(?:[a-fA-F\d]{1,4}:)*(?:[a-fA-F\d]{1,4}|\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}))?)\]))(?::(\d*))?))? (?# 4: host, 5: port)
|
@nudded
nudded / day3.rb
Created December 3, 2017 19:29
Day 3
MOVE_ARRAY = [[0,1],[-1,0],[0,-1],[1,0]]
ODD_SQUARES = (1..10000).select(&:odd?).map {|i| i * i}
# move the pointer in the spiral to the next position
def move(counter, x, y)
# we should go right if we are a square number
sqrt_counter = Math.sqrt(counter).to_i
sqrt_counter = sqrt_counter.odd? ? sqrt_counter : sqrt_counter - 1
return [x+1, y] if ODD_SQUARES.include? counter
lowest_right_corner = sqrt_counter * sqrt_counter
move_array_index = (1..4).detect do |i|
@nudded
nudded / day5.rb
Created December 5, 2017 19:27
day5.rb
input = File.readlines("inputs/day5.in")
stack = input.map &:to_i
def move(array, position)
offset = array[position]
if offset >=3
array[position] -= 1
else
array[position] += 1
end
@nudded
nudded / day7.rb
Created December 7, 2017 19:43
day7.rb
input = File.readlines("inputs/day7.in")
DISCS = {}
class Disc < Struct.new(:name, :weight, :children, :parent)
def subtree_weight
return weight unless children
children.map {|c| DISCS[c].subtree_weight}.inject(0) {|a,b| a+b} + weight
end
end