diff options
-rwxr-xr-x | day05/part1 | 31 | ||||
-rwxr-xr-x | day05/part2 | 30 |
2 files changed, 26 insertions, 35 deletions
diff --git a/day05/part1 b/day05/part1 index 4761380..9fe581d 100755 --- a/day05/part1 +++ b/day05/part1 @@ -2,26 +2,21 @@ input = gets.chomp.chars -l = input.length -loop do - i = input.length - 1 - loop do - break if i < 1 +polymer = input +nextpolymer = Array.new(polymer.length) +nextpolymer[0] = polymer[0] +index = 1 +nextindex = 0 - if input[i] == input[i-1].swapcase - input[i] = nil - input[i-1] = nil - i -= 2 - else - i -= 1 - end - end - input = input.compact - if input.length == l - break +while index < polymer.length do + if polymer[index].swapcase != nextpolymer[nextindex] + nextindex += 1 + nextpolymer[nextindex] = polymer[index] else - l = input.length + nextpolymer[nextindex] = nil + nextindex -= 1 end + index += 1 end -puts input.length +puts nextpolymer.compact.length diff --git a/day05/part2 b/day05/part2 index b71d84e..d09ff86 100755 --- a/day05/part2 +++ b/day05/part2 @@ -8,27 +8,23 @@ lengths = {} units.each do |unit| polymer = input.dup polymer.delete_if {|u| u == unit || u == unit.upcase} - l = polymer.length - loop do - i = polymer.length - 1 - loop do - break if i < 1 + nextpolymer = Array.new(polymer.length) + nextpolymer[0] = polymer[0] + index = 1 + nextindex = 0 - if polymer[i] == polymer[i-1].swapcase - polymer.delete_at(i) - polymer.delete_at(i-1) - i -= 2 - else - i -= 1 - end - end - if polymer.length == l - break + while index < polymer.length do + if polymer[index].swapcase != nextpolymer[nextindex] + nextindex += 1 + nextpolymer[nextindex] = polymer[index] else - l = polymer.length + nextpolymer[nextindex] = nil + nextindex -= 1 end + index += 1 end - lengths[unit] = polymer.length + + lengths[unit] = nextpolymer.compact.length end puts lengths.values.min |