Created
December 15, 2020 16:36
-
-
Save SleeplessByte/f6d07ba6835b07dbc0f3cf3a5cef7f22 to your computer and use it in GitHub Desktop.
Advent of Code 2020: Day 15 - Rambunctious Recitation
This file contains hidden or 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
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 |
This file contains hidden or 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
0,3,6 |
This file contains hidden or 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
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