Created
May 7, 2011 23:20
-
-
Save basicxman/960952 to your computer and use it in GitHub Desktop.
Google Code Jam Qualification Rounds Problem #3
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
| #!/usr/bin/env ruby | |
| data = [] | |
| file_data = File.read(ARGV[0]).split("\n") | |
| is_case_line = false | |
| 1.upto(file_data.length - 1) do |i| | |
| data << file_data[i].split(" ").map { |n| n.to_i } if is_case_line | |
| is_case_line = (is_case_line) ? false : true | |
| end | |
| def decimal_to_binary(number) | |
| number.to_s(2) | |
| end | |
| def sum(a, b) | |
| total = eval("0b#{a.to_s(2)}^0b#{b.to_s(2)}") | |
| end | |
| def rest(full_arr, sub_arr) | |
| temp = full_arr.dup | |
| sub_arr.each { |val| temp.slice! temp.index(val) } | |
| temp | |
| end | |
| output_file = File.open("output.txt", "w") | |
| data.each_with_index do |arr, case_number| | |
| largest = 0 | |
| a_index = 1 | |
| b_index = arr.length - 1 | |
| until a_index > b_index | |
| left_sides = arr.combination(a_index).to_a | |
| left_sides.each do |side| | |
| right_side = rest(arr, side) | |
| seans_total = side.inject { |current_sum, n| sum(current_sum, n) } | |
| patricks_total = right_side.inject { |current_sum, n| sum(current_sum, n) } | |
| seans_real_total = side.inject { |current_sum, n| current_sum + n } | |
| patricks_real_total = right_side.inject { |current_sum, n| current_sum + n } | |
| if seans_total == patricks_total | |
| if seans_total > patricks_total | |
| largest = seans_real_total if seans_real_total > largest | |
| else | |
| largest = patricks_real_total if patricks_real_total > largest | |
| end | |
| end | |
| end | |
| a_index += 1 | |
| b_index -= 1 | |
| end | |
| value = (largest == 0) ? "NO" : largest | |
| ending = (case_number + 1 == data.length) ? "" : "\n" | |
| output_file.print "Case ##{case_number + 1}: #{value}#{ending}" | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment