Skip to content

Instantly share code, notes, and snippets.

View nudded's full-sized avatar

Toon Willems nudded

View GitHub Profile
list = 10000000.times.map {rand(10)}
def answer(list)
amount_of_zeros = 0
index = list.size - 1
while index >= 0
if list[index].zero?
amount_of_zeros += 1
elsif amount_of_zeros > 0
list[index], list[index+amount_of_zeros] = list[index+amount_of_zeros], list[index]
❯ rbenv local 2.6.5
~
❯ ruby test.rb
true
Rehearsal -------------------------------------------
answer1 0.892859 0.026647 0.919506 ( 0.921522)
answer2 0.890985 0.035343 0.926328 ( 0.927158)
---------------------------------- total: 1.845834sec
#! /usr/bin/env bash
. /home/runner/.bash_profile
ruby_version=${1:-"2.3.1"}
ruby_archive="$ruby_version.tar.gz"
ruby_install_path="/home/runner/.rbenv/versions/$ruby_version"
echo "*****************************************"
echo "Setting up Ruby $ruby_version"
#!/usr/bin/env bash
###
# Usage:
# Add the following set of commands to the setup of a build in the Project Settings
#
# sudo apt-get purge -y postgresql-client-* postgresql-* postgresql-contrib-* postgresql-server-dev-* libpq-dev
# sudo apt-get update
# wget https://gist.githubusercontent.com/mimimalizam/b9ee28d74e1a59d58719916f42c18226/raw/pg_semaphore.sh && bash pg_semaphore.sh
#
@nudded
nudded / day8.rb
Created December 8, 2017 21:44
day8.rb
input = File.readlines('inputs/day8.in')
REGISTERS = Hash.new {|h,k| h[k] = Register.new(k, 0,0)}
class Register < Struct.new(:name, :value, :highest_value); end
class Condition < Struct.new(:register, :condition, :value)
def check
case condition
when ">"
@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
@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 / 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|
{: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)
|
{-# 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