Created
December 6, 2016 06:13
-
-
Save sylph01/5d07c2a24898d8b3703fb624204c8fb3 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
def calculate(actions) | |
if actions.all?{ |c| c == 'O'} | |
return 2 ** (actions.count + 2) - 4 | |
elsif actions.all?{ |c| c == 'X'} | |
if actions.count == 1 | |
return 5 | |
else | |
return 4 ** (actions.count - 1) + 1 | |
end | |
else | |
num_of_xs = actions.select{ |c| c == 'X'}.size | |
num_of_ys = actions.size - num_of_xs | |
if actions.last == 'X' | |
if num_of_xs >= 2 | |
4 ** (num_of_xs - 1) * 5 ** num_of_ys + 1 | |
else | |
5 ** num_of_ys + 5 | |
end | |
else | |
# last is 'O' | |
last, *rest = actions.reverse | |
calculate(rest.reverse) + 4 * (2 ** (num_of_ys - 1)) | |
end | |
end | |
end | |
def test(actions_str, expected) | |
actions = actions_str.split("") | |
result = calculate(actions) | |
if result != expected.to_i | |
puts "#{actions_str} : #{result} vs #{expected}" | |
end | |
end | |
File.readlines("input_samples").map{ |x| x.chomp.split(",") }.each do |x| | |
test(x[0], x[1]) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment