m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/15/b.rb
blob: 7c7b3f9c74da62a013466af0a7e375daaa2fcc7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
input = File.read('input.txt').split ','

@t = 1

last_spoken = {}
last_number = 0
input.each_with_index do |n, i|
  if i > 0
    last_spoken[last_number] = i
  end
  last_number = n.to_i
end

(input.length+1..30000000).each do |turn|
  if !last_spoken[last_number]
    last_spoken[last_number] = turn - 1
    last_number = 0
  else
    new_last_number = (turn - 1) - last_spoken[last_number]
    last_spoken[last_number] = turn - 1
    last_number = new_last_number
  end
end

puts last_number