AoC2015/day10/solution.rb

45 lines
928 B
Ruby

#!/bin/env ruby
require 'pry'
input = File.read('./input').strip
# BADBADBAD takes sooooooo long at big numbers
# def look_and_say(sequence)
# sequence += 'E'
# result = ''
# sequence.chars.each_with_index do |num, i|
# next if result[-1] == num or num == 'E'
# n = i
# while sequence.chars[i..n].uniq.count == 1
# n += 1
# end
# result += (n - i).to_s
# result += num
# end
# result
# end
# Stole this :(
def look_and_say(number)
result = ''
i = 0
while i < number.length
c = number[i]
j = i
j += 1 while j < number.length && number[j] == c
result << "#{j - i}#{c}"
i = j
end
result
end
# Change to 40 times for part 1
sequences = [input]
50.times do |i|
puts "Run #{i} is #{sequences[-1].length}"
sequences.push(look_and_say(sequences[-1]))
end
puts "Part 2 Solution: #{sequences[-1].length}"
# binding.pry