Skip to content

Instantly share code, notes, and snippets.

@ajmurmann
Last active December 8, 2016 06:03
Show Gist options
  • Save ajmurmann/a3f4d7585e861ef5d64f9b5bb8136ba3 to your computer and use it in GitHub Desktop.
Save ajmurmann/a3f4d7585e861ef5d64f9b5bb8136ba3 to your computer and use it in GitHub Desktop.
advent8
text=File.open('8.txt').read
m = [
Array.new(50,0),
Array.new(50,0),
Array.new(50,0),
Array.new(50,0),
Array.new(50,0),
Array.new(50,0),
]
def rect(m, line)
x,y = (%r|rect (\d+)x(\d+)|).match(line)[1,2]
(0..(y.to_i - 1)).each do |i|
(0..(x.to_i - 1)).each do |j|
m[i][j] = 1
end
end
end
def rotate_row(m, line)
y, n = (%r|rotate row y=(\d+) by (\d+)|).match(line)[1,2]
y = y.to_i
n = n.to_i
row = m[y]
i = -1
m[y] = row.map {|v| i += 1; row[(i-n)%row.size]}
end
def rotate_column(m, line)
x, n = (%r|rotate column x=(\d+) by (\d+)|).match(line)[1,2]
x = x.to_i
n = n.to_i
i = -1
new_values = (0..m.size).map do
i += 1
m[(i-n)%m.size][x]
end
(0..(m.size - 1)).each do |i|
m[i][x] = new_values[i]
end
end
text.lines.each do |line|
rect(m, line)if line.include?('rect')
rotate_row(m, line) if line.include?('rotate row')
rotate_column(m, line) if line.include?('rotate column')
m.each do |y|
puts y.map{|c| c == 0 ? ' ' : '▩'}.join('')
end
puts "=================="
end
puts m.inject(0){|agg, row| agg+row.inject(0,&:+) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment