Sha256: ead462b98d0f28612e96598ab8585edf70f87b38554fb417fd85f38099691737

Contents?: true

Size: 1.01 KB

Versions: 3

Compression:

Stored size: 1.01 KB

Contents

#
# https://adventofcode.com/2018/day/9 part 1 and part 2
#
# Does not use any cem functions \o/, but did you know that 
#  - Array#shift is supposed to be fast, but is not fast enough for this problem?
# 

players = 428 
marbles = 70825

def day9(players, marbles)

  scores = [0] * players

  nexta = 3
  storage = [0, 2, 1] 
  current = 1
  front = 0

  while nexta < marbles 

    if nexta % 23 == 0
      
      current = (current + storage.size - 7) % storage.size
      worth = nexta + storage.delete_at(current)
      scores[nexta % players] += worth
    
    else
    
      # Avoid wrapping the index
      while current + 2 > storage.size
        front_value = storage[front]
        front += 1
        storage.push front_value  
      end
      
      current += 2
      storage.insert(current, nexta)
      
    end
    
    nexta += 1
    
  end
  
  return scores.max
end  
  
puts "One Star : #{day9(players, marbles)}"
puts "Two Stars: #{day9(players, marbles * 100)}"

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
cem-0.1.7 examples/aoc2018/day9.rb
cem-0.1.6 examples/aoc2018/day9.rb
cem-0.1.5 examples/aoc2018/day9.rb