Skip to content

Instantly share code, notes, and snippets.

View sarahzrf's full-sized avatar

sarahzrf sarahzrf

View GitHub Profile
def neighbors(grid, x, y)
ns = []
ns << grid[y][x - 1] if x > 0
ns << grid[y][x + 1] if x + 1 < grid[y].length
ns << grid[y - 1][x] if y > 0
ns << grid[y + 1][x] if y + 1 < grid.length
ns
end
map = $<.map {|l| l.chomp.chars.map(&:to_i)}
require 'set'
def group(obs_raw)
obs = Hash.new {Set.new("abcdefg".chars)}
obs_raw.each do |word|
obs[word.length] &= Set.new(word.chars)
end
obs
end
count = 0
$<.each do |l|
outs = l.chomp.split(' | ')[1].split
count += outs.count {|o| [2, 4, 3, 7].include? o.length}
end
print count
class Integer
def sumtorial
(0..self).sum
end
end
def cost(counts, x0)
counts.sum {|x, n| (x0 - x).abs.sumtorial * n}
end
def cost(counts, x0)
counts.sum {|x, n| (x0 - x).abs * n}
end
count_by_pos = Hash.new(0)
gets.chomp.split(',').map(&:to_i).each do |x|
count_by_pos[x] += 1
end
def cost(counts, x0)
counts.sum {|x, n| (x0 - x).abs * n}
end
count_by_pos = Hash.new(0)
gets.chomp.split(',').map(&:to_i).each do |x|
count_by_pos[x] += 1
end
def advance(fish)
new_fish = Hash.new(0)
fish.each do |timer, count|
if timer > 0
new_fish[timer - 1] += count
elsif timer == 0
new_fish[6] += count
new_fish[8] += count
else
raise "wtf?"
def range(i, j)
return (i..j).each if i <= j
Enumerator.new do |y|
loop do
y << i
break if i == j
i -= 1
end
end
end
lines = $<.map do |l|
l =~ /(\d+),(\d+) -> (\d+),(\d+)/
[[$1.to_i, $2.to_i], [$3.to_i, $4.to_i]]
end
map = Hash.new(0)
lines.each do |(x1, y1), (x2, y2)|
x1, x2 = [x1, x2].sort
y1, y2 = [y1, y2].sort
lines = $<.map do |l|
l =~ /(\d+),(\d+) -> (\d+),(\d+)/
[[$1.to_i, $2.to_i], [$3.to_i, $4.to_i]]
end
map = Hash.new(0)
lines.each do |(x1, y1), (x2, y2)|
if x1 == x2
x = x1