Skip to content

Instantly share code, notes, and snippets.

@MikeRogers0
Created December 10, 2020 11:16
Show Gist options
  • Save MikeRogers0/dbdefb907ba7923b7426cd7e59418ff7 to your computer and use it in GitHub Desktop.
Save MikeRogers0/dbdefb907ba7923b7426cd7e59418ff7 to your computer and use it in GitHub Desktop.
Advent Of Code 202 - day 9
def number_is_not_sum_of_previous_preamble?(index)
numbers = @numbers[(index - 25)...(index)]
aim_number = @numbers[index]
numbers.each do |number_1|
numbers.each do |number_2|
combined_number = number_1 + number_2
return true if combined_number == aim_number
end
end
false
end
def combined_numbers_equals_aim_number(index)
numbers = @numbers[...(index)]
aim_number = @numbers[index]
numbers.each_with_index do |number, end_index|
combined_number = 0
start_index = end_index
while( (combined_number && combined_number <= aim_number ) || start_index >= 0) do
combined_number = numbers[start_index...end_index].inject(&:+)
if combined_number == aim_number
congatiguous_numbers = numbers[start_index...end_index]
lowest_number = congatiguous_numbers.min
highest_number = congatiguous_numbers.max
puts "Numbers are: #{lowest_number} & #{highest_number}"
puts "Combined: #{lowest_number + highest_number}"
return
end
start_index -= 1
end
end
end
@numbers = File.open('input.real.txt').read.split("\n").compact.collect(&:to_i)
@numbers.each_with_index do |number, index|
next if index < 25
puts "number is: #{number}" unless number_is_not_sum_of_previous_preamble?(index)
unless number_is_not_sum_of_previous_preamble?(index)
combined_numbers_equals_aim_number(index)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment