Sha256: ee0f127c6c9291194cb5cd7032357ffcb758d87a3dbf3fd028610bf8b693f6b4

Contents?: true

Size: 1.12 KB

Versions: 4

Compression:

Stored size: 1.12 KB

Contents

#
# https://adventofcode.com/2018/day/8 part 2
#
# Does not use any cem functions \o/ 
# 

input = File.read("inputs/day8_input.txt").split

stack = []
index = 0

while index < input.size

  n_children = input[index].to_i
  index += 1
  n_metaData = input[index].to_i
  index += 1
  
  if n_children == 0
  
    metadata = 0
    n_metaData.times { 
      metadata += input[index].to_i
      index += 1
    }
  
    stack.last[2] << metadata
    stack.last[0] -= 1
    
    while stack.last.first == 0
  
      metadata = 0
      stack.last[1].times { 
        selector = input[index].to_i - 1
        index += 1
        
        if selector < stack.last[2].size
          metadata += stack.last[2][selector]
        end        
      }
      
      stack.pop
      
      if stack.size > 0
        stack.last.last << metadata
      else
        puts metadata
        exit
      end
      
    end    
  
  else
  
    if stack.size > 0
      stack.last[0] -= 1
    end
    stack << [n_children, n_metaData, []]
    
    # puts stack.inspect
  
  end    
end
    
puts stack.inspect

Version data entries

4 entries across 4 versions & 1 rubygems

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