Skip to content

Instantly share code, notes, and snippets.

@BertZZ
Last active December 4, 2021 02:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BertZZ/1993cec70a282c8223c0ed0fc065a004 to your computer and use it in GitHub Desktop.
Save BertZZ/1993cec70a282c8223c0ed0fc065a004 to your computer and use it in GitHub Desktop.
Advent of Code Day 3 part 2 find Oxygen method
def find_oxygen(data)
input = data.clone
bits = input[0].length
for i in 0..bits - 1
ones = input.count { |n| n[i] == "1" }
zeros = input.count { |n| n[i] == "0" }
most_common = ones > zeros ? "1" : "0"
most_common = "1" if ones == zeros
oxygen = input.delete_if {|binary| binary[i] != most_common}.first
break if input.count == 1
end
oxygen.to_i(2)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment