Created
September 27, 2022 17:34
-
-
Save d12/bca8073e3f3240575afb7e49ad24457f 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
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