Skip to content

Instantly share code, notes, and snippets.

@SleeplessByte
Created December 15, 2020 16:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SleeplessByte/f6d07ba6835b07dbc0f3cf3a5cef7f22 to your computer and use it in GitHub Desktop.
Save SleeplessByte/f6d07ba6835b07dbc0f3cf3a5cef7f22 to your computer and use it in GitHub Desktop.
Advent of Code 2020: Day 15 - Rambunctious Recitation
numbers = File.read('input.txt').chomp.split(',').map(&:to_i)
turns = numbers.dup
memory = Hash.new { [] }
numbers.each_with_index do |number, turn|
memory[number] = [turn]
puts "Turn #{turn + 1}: #{number}"
end
goal = 30000000
while turns.length < goal
turn = turns.length
previous = turns.last
before_last, last = memory[previous][-2..]
speak = last.nil? ? 0 : last - before_last
# You could store only the last two numbers, but with this set of data there
# is really no point
memory[speak] = memory[speak].push(turn)
puts "Turn #{turn + 1}, speaking #{speak} (#{(turn / goal.to_f * 100).round}%)" if (turn + 1) % (goal / 100) == 0
turns.push speak
end
puts turns.last
11,18,0,20,1,7,16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment