Skip to content

Instantly share code, notes, and snippets.

@epitron
Created December 6, 2015 22:12
Show Gist options
  • Save epitron/cfed04032e8f21be5610 to your computer and use it in GitHub Desktop.
Save epitron/cfed04032e8f21be5610 to your computer and use it in GitHub Desktop.
m = Array.new(1000) { |a| Array.new(1000, 0) }
transformations = {
"toggle" => proc { |input| input+2 },
"turn on" => proc { |input| input+1 },
"turn off" => proc { |input| [0, input-1].max },
}
open("advent6.txt") do |f|
f.each_line do |line|
if line =~ /(toggle|turn on|turn off) (\d+),(\d+) through (\d+),(\d+)/
command = $1
x1, y1, x2, y2 = [$2, $3, $4, $5].map(&:to_i)
transform = transformations[command]
puts line
(y1..y2).each { |y| (x1..x2).each { |x| m[y][x] = transform[m[y][x]] } }
end
end
end
p m.map { |row| row.reduce(:+) }.reduce(:+)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment