Created
March 10, 2018 13:31
-
-
Save angusluk/5a550f100f71ef6e379cf0812650105c to your computer and use it in GitHub Desktop.
My solution of Google codegem 2017 - Problem A. Oversized Pancake Flipper
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 flip(string, fliper_size, flip_at) | |
i = 0 | |
if (flip_at + fliper_size) <= string.length | |
while i < fliper_size | |
if string[flip_at + i] == '-' | |
string[flip_at + i] = '+' | |
elsif string[flip_at + i] = '+' | |
string[flip_at + i] = '-' | |
end | |
i += 1 | |
end | |
end | |
return string | |
end | |
line_num = 0 | |
fliper_size = 0 | |
num_of_row = 0 | |
text=File.open('A-large-practice.in').read | |
text.gsub!(/\r\n?/, "\n") | |
text.each_line.with_index do |line, index| | |
if index ==0 | |
num_of_row = line | |
else | |
num_of_flip = 0 | |
array = line.chomp.split(' ', 2) | |
line = array[0] | |
fliper_size = array[1] | |
i = 0 | |
while i < line.length | |
if line[i] == '-' | |
line = flip(line, fliper_size.to_i, i) | |
num_of_flip += 1 | |
end | |
i += 1 | |
end | |
if line.include? '-' | |
open('output.txt', "a") { |f| f << "Case ##{index}: IMPOSSIBLE\n" } | |
else | |
open('output.txt', "a") { |f| f << "Case ##{index}: #{num_of_flip}\n" } | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment