Created
February 13, 2012 22:32
-
-
Save iMagesh/1821026 to your computer and use it in GitHub Desktop.
watch tv
This file contains hidden or 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
@low_channel = 1 # lowest channel | |
@high_channel = 10 # highest channel | |
@blocked = [2, 6, 8] # blocked channels | |
@channels_to_visit = [9,1,2,3,9] #channels to watch | |
flash = 0 | |
click1 = 0 | |
click2 = 0 | |
click3 = 0 | |
click4 = 0 | |
current_channel = 0 | |
@channels = (@low_channel..@high_channel).to_a | |
@allowed_chn = @channels - @blocked | |
@pos = 0 | |
def flash(ch) | |
if @channels_to_visit.index(ch) > 0 | |
flash = @channels_to_visit[@pos] | |
@pos = @pos + 1 | |
elsif @channels_to_visit.index(ch) > 1 | |
flash = @channels_to_visit[@pos] | |
@pos = @pos + 1 | |
else | |
flash = @channels_to_visit[@pos] | |
end | |
return flash | |
end | |
def next_route(current_channel) | |
next_range = @allowed_chn | |
while next_range.first != current_channel do | |
next_range = next_range.push(next_range.shift) | |
end | |
return next_range | |
end | |
def prev_route(current_channel) | |
allowed_chn = [1,3,4,5,7,9,10] | |
prev_range = @allowed_chn.reverse | |
while prev_range.first != current_channel do | |
prev_range = prev_range.push(prev_range.shift) | |
end | |
return prev_range | |
end | |
def flash_up(ch, flash) | |
rflash_range = @allowed_chn | |
while rflash_range.first != flash do | |
rflash_range = rflash_range.push(rflash_range.shift) | |
end | |
return rflash_range | |
end | |
def flash_down(ch, flash) | |
lflash_range = @allowed_chn.reverse | |
while lflash_range.first != flash do | |
lflash_range = lflash_range.push(lflash_range.shift) | |
end | |
return lflash_range | |
end | |
def lowest_clicks(a, b, c, d, e) | |
arr = [a, b, c, d] | |
arr = arr.compact | |
best = arr[0] | |
arr.each do |i| | |
next if i==0 | |
if i < best | |
best = i | |
end | |
end | |
return best | |
end | |
@channels_to_visit.each do |ch| | |
dummy = 0 | |
if !@allowed_chn.include?(ch) | |
puts "Sorry channel #{ch} is blocked!" | |
next | |
elsif @channels_to_visit.index(ch) == 0 && dummy == 0 | |
next_range = (current_channel..@allowed_chn.last).to_a | |
prev_range = prev_route(ch) | |
lflash_range = flash_down(ch, flash) if flash > 0 | |
rflash_range = flash_up(ch, flash) if flash > 0 | |
click = ch.to_s.split("").size | |
current_channel = ch | |
# puts "#{click} click for channel #{ch}" | |
else | |
dummy = 2 | |
next_range = next_route(current_channel) | |
prev_range = prev_route(current_channel) | |
lflash_range = flash_down(ch, flash) if flash > 0 | |
rflash_range = flash_up(ch, flash) if flash > 0 | |
if next_range.include?(ch) | |
if next_range.index(ch) > next_range.index(current_channel) | |
click1 = (next_range.index(ch) + 1) - (next_range.index(current_channel) + 1) | |
else | |
click1 = (next_range.index(current_channel) + 1) - (next_range.index(ch) + 1) | |
end | |
# puts "#{click1} clicks for channel #{ch}" | |
end | |
if prev_range.include?(ch) | |
if prev_range.index(ch) > prev_range.index(current_channel) | |
click2 = (prev_range.index(ch) + 1) - (prev_range.index(current_channel) + 1) | |
else | |
click2 = (prev_range.index(current_channel) + 1) - (prev_range.index(ch) + 1) | |
end | |
# puts "#{click2} clicks for channel #{ch}" | |
end | |
if lflash_range && lflash_range.include?(ch) | |
if lflash_range.index(ch) > lflash_range.index(lflash_range.first) | |
click3 = (lflash_range.index(ch) + 1) - (lflash_range.index(lflash_range.first) + 1) | |
else | |
click3 = (lflash_range.index(lflash_range.first) + 1) - (lflash_range.index(ch) + 1) | |
end | |
click3 = click3 + 1 # 1 count for flash button | |
# puts "#{click3} clicks for channel #{ch}" | |
end | |
if rflash_range && rflash_range.include?(ch) | |
if rflash_range.index(ch) > rflash_range.index(rflash_range.first) | |
click4 = (rflash_range.index(ch) + 1) - (rflash_range.index(rflash_range.first) + 1) | |
else | |
click4 = (rflash_range.index(rflash_range.first) + 1) - (rflash_range.index(ch) + 1) | |
end | |
click4 = click4 + 1 # 1 count for flash button | |
# puts "#{click4} clicks for channel #{ch}" | |
end | |
current_channel = ch | |
end | |
flash = flash(ch) #store channel in flash | |
click_arr = [click, click1, click2, click3, click4] | |
best_click = lowest_clicks(click, click1, click2, click3, click4) | |
puts "Lowest clicks to reach channel #{ch} is #{best_click}" | |
end | |
#print "total clicks while watching" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment