This file contains 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
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)} |
This file contains 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
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 |
This file contains 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
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 | |
This file contains 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 Integer | |
def sumtorial | |
(0..self).sum | |
end | |
end | |
def cost(counts, x0) | |
counts.sum {|x, n| (x0 - x).abs.sumtorial * n} | |
end |
This file contains 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
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 |
This file contains 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
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 |
This file contains 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
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?" |
This file contains 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
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 |
This file contains 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
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 |
This file contains 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
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 |