Sha256: 5af175148a21291290e642e84f0464e7d0bc023047de1ddf9eadd8cd22dfae93

Contents?: true

Size: 658 Bytes

Versions: 4

Compression:

Stored size: 658 Bytes

Contents

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

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

stack = []
index = 0
sum = 0

while index < input.size

  n_children = input[index].to_i
  index += 1
  n_metaData = input[index].to_i
  index += 1
  
  # f.subpane("out").replace "#{n_children}, #{n_metaData}"
  
  stack << [n_children, n_metaData]
  
  while stack.size > 0 && stack.last.first == 0
    stack.last.last.times { 
      sum += input[index].to_i
      index += 1
    }
    
    stack.pop
  end
  
  if stack.size > 0
    stack.last[0] -= 1
  end
  
end

puts sum

Version data entries

4 entries across 4 versions & 1 rubygems

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