aboutsummaryrefslogtreecommitdiff
path: root/rb/day12.rb
blob: 96fef5f463782e19a7631f1759f2318438be03aa (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
26
27
28
#!/usr/bin/env ruby

input = File.read("day12.input")

puts "Part 1: #{input.scan(/-?\d+/).map(&:to_i).sum}"

require 'json'
json = JSON.parse(input)

search = [json]
sum = 0

until search.empty?
  this = search.shift
  case this
  when Numeric
    sum += this
  when Array
    search.push(*this)
  when Hash
    unless this.values.include?("red")
      search.push(*this.keys)
      search.push(*this.values)
    end
  end
end

puts "Part 2: #{sum}"