Create a gist now

Instantly share code, notes, and snippets.

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