Skip to content

Instantly share code, notes, and snippets.

@Clashbuster
Created December 20, 2020 21:30
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 Clashbuster/722338797dc16958652f7c3980accb4d to your computer and use it in GitHub Desktop.
Save Clashbuster/722338797dc16958652f7c3980accb4d to your computer and use it in GitHub Desktop.
class Computer
def initialize (filename)
@data = IO.read(filename).lines.map(&:chomp)
@record = {}
@turn = 1
@previous = {
'exotic' => true,
'num' => 0
}
end
def simulate
start = @data[0].split(",").map { |num| num.to_i}
start.each do |num|
@record[num] = {
'recent' => @turn,
'earlier' => @turn
}
@previous = {'exotic' => true, 'num' => num}
@turn +=1
end
while @turn < 30000001 do
if @turn % 15000 === 0
File.open("./15.log.txt", "a") { |f| f.write "Computed 15000 turns\n\n" }
end
if @previous['exotic']
if @record[0]
@record[0]['earlier'] = @record[0]['recent']
@record[0]['recent'] = @turn
@previous = {'exotic' => false, 'num' => 0}
else
@record[0]= {
'recent' => @turn,
'earlier' => @turn
}
@previous = {'exotic' => true, 'num' => 0}
end
else
new_num = (@turn - 1) - @record[@previous['num']]['earlier']
if @record[new_num]
@record[new_num]['earlier'] = @record[new_num]['recent']
@record[new_num]['recent'] = @turn
@previous = {'exotic' => false, 'num' => new_num}
else
@record[new_num] = {
'recent' => @turn,
'earlier' => @turn
}
@previous = {'exotic' => true, 'num' => new_num}
end
end
@turn +=1
end
return @previous
end
end
c1 = Computer.new ('15.input.txt')
starting = Time.now
p c1.simulate
ending = Time.now
p "computed in #{(ending - starting).floor} seconds"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment