Skip to content

Instantly share code, notes, and snippets.

@d12
Created September 27, 2022 17:34
Show Gist options
  • Save d12/bca8073e3f3240575afb7e49ad24457f to your computer and use it in GitHub Desktop.
Save d12/bca8073e3f3240575afb7e49ad24457f to your computer and use it in GitHub Desktop.
f_lines = File.read("input.txt").lines.map(&:chomp)
result_lines = f_lines.map do |line|
# Extract all angle brackets, and sort them.
# We use this as a "stack" of angle brackets that we can pull from
# when we need an angle bracket in the final result
angle_brackets = line.scan(/[<>]/).sort
line_chars = line.chars.map do |c|
c == " " ? " " : angle_brackets.shift
end
line_chars.join
end
puts result_lines.join("\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment