Created
December 6, 2015 17:57
-
-
Save eam/22da7a9d9ebf7a867a0d to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env ruby | |
map = {} | |
xpos = 0 | |
ypos = 0 | |
mech = { | |
"turn on" => :turn_on, | |
"turn off" => :turn_off, | |
"toggle" => :toggle, | |
} | |
def turn_on(x) | |
# true | |
return 1 if x.nil? | |
x + 1 | |
end | |
def turn_off(x) | |
# false | |
return 0 if x.nil? or x == 0 | |
x - 1 | |
end | |
def toggle(x) | |
# !x | |
return 2 if x.nil? | |
x + 2 | |
end | |
[*0..1000].each { |i| map[i] = {} } | |
STDIN.each_line do |l| | |
l =~ /(turn on|turn off|toggle)\s+(\d+),(\d+) through (\d+),(\d+)/ or raise "couldn't parse: #{l}" | |
mech_func = mech[$1] | |
[*$2.to_i..$4.to_i].each do |x| | |
[*$3.to_i..$5.to_i].each do |y| | |
map[x][y] = send mech_func, map[x][y] | |
end | |
end | |
end | |
sum = 0 | |
[*0..1000].each do |x| | |
[*0..1000].each do |y| | |
sum += map[x][y] if map[x][y] | |
end | |
end | |
puts sum |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment