This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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| |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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| |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# http://adventofcode.com/day/15 | |
require "forwardable" | |
Ingredient = Struct.new(:coefficients, :calories) | |
class Ingredients | |
extend Forwardable |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# http://adventofcode.com/day/6 | |
class Lights | |
def initialize | |
@lights = HEIGHT.times.map do | |
[false] * WIDTH | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# http://adventofcode.com/day/12 | |
require "json" | |
# part 1 | |
puts File.read("input").scan(/-?\d+/).map(&:to_i).reduce(0, &:+) |