Skip to content

Instantly share code, notes, and snippets.

View wconrad's full-sized avatar

Wayne Conrad wconrad

View GitHub Profile
@wconrad
wconrad / dbm_concurrent_iteration_anomaly
Last active May 16, 2016 14:18
Iterating over second dbm file prematurely ends iteration over first dbm file
#!/usr/bin/env ruby
# Reproduce an anomaly in Ruby's built-in dbm library: When iterating
# over a DBM file, accessing a second dbm file's enumerator
# prematurely ends the first file's iteration.
require "dbm"
require "tmpdir"
system("ruby -v") # => ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-linux]
@wconrad
wconrad / sqlserver_adapter.rb
Created February 23, 2016 13:03
Example of a monkey patch
# frozen_string_literal: true
require "activerecord-sqlserver-adapter"
# Monkey-patch the SqlServer/ActiveRecord adapter to set
# certain connection options.
#
# --- SET ARITHABORT ON
#
# SqlServer, when a tcp/ip connection is made, sets some default
@wconrad
wconrad / day20_part1.rb
Last active December 20, 2015 14:30
Advent of Code, Day 20
#!/usr/bin/env ruby
# http://adventofcode.com/day/20
input = 34000000
def deliver_presents(num_houses)
presents = [0] + [0] * num_houses
(1..num_houses).each do |elf|
(elf..num_houses).step(elf) do |house_number|
@wconrad
wconrad / day17.rb
Created December 17, 2015 10:54
Advent of Code, day 17
#!/usr/bin/env ruby
# http://adventofcode.com/day/17
def containers_totalling(containers, target_sum)
Enumerator.new do |yielder|
(0...(2 ** containers.size)).map do |mask|
selected_containers = containers.select.with_index do |container, bitnum|
(mask & (1 << bitnum)) != 0
end
@wconrad
wconrad / day16_part1.rb
Created December 16, 2015 13:31
Advent of Code, day 16
#!/usr/bin/env ruby
# http://adventofcode.com/day/16
Sue = Struct.new(:id, :attrs)
sues = File.read("input").each_line.map do |line|
id = Integer(line[/(\d+)/])
attrs = Hash[
line.scan(/(\w+): (\d+)/).map do |key, value|
@wconrad
wconrad / day15.rb
Created December 16, 2015 01:57
Advent of Code, day 15
# http://adventofcode.com/day/15
require "forwardable"
Ingredient = Struct.new(:coefficients, :calories)
class Ingredients
extend Forwardable
@wconrad
wconrad / day6.rb
Created December 15, 2015 21:45
Advent of Code, day 6
#!/usr/bin/env ruby
# http://adventofcode.com/day/6
class Lights
def initialize
@lights = HEIGHT.times.map do
[false] * WIDTH
end
@wconrad
wconrad / day14.rb
Created December 15, 2015 00:00
Advent of Code, day 14
class Raindeer
attr_reader :name
def initialize(name:, speed:, sprint_time:, rest_time:)
@name = name
@speed = speed
@sprint_time = sprint_time
@rest_time = rest_time
end
@wconrad
wconrad / day13.rb
Created December 13, 2015 09:26
Advent of Code, day 13
#!/usr/bin/env ruby
# http://adventofcode.com/day/13
INPUT_PATTERN = /^(\w+) would (gain|lose) (\d+) happiness units by sitting next to (\w+).$/
@deltas = Hash[
File.read("input").lines.map do |line|
raise line.inspect unless line =~ INPUT_PATTERN
person, sign, delta, neighbor = $~.captures
@wconrad
wconrad / day12.rb
Created December 12, 2015 13:06
Advent of Code, day 12
#!/usr/bin/env ruby
# http://adventofcode.com/day/12
require "json"
# part 1
puts File.read("input").scan(/-?\d+/).map(&:to_i).reduce(0, &:+)