Skip to content

Instantly share code, notes, and snippets.

@wconrad
Created December 12, 2015 13:06
Show Gist options
  • Save wconrad/34d3ffebbdb8d8537fbc to your computer and use it in GitHub Desktop.
Save wconrad/34d3ffebbdb8d8537fbc to your computer and use it in GitHub Desktop.
Advent of Code, day 12
#!/usr/bin/env ruby
# http://adventofcode.com/day/12
require "json"
# part 1
puts File.read("input").scan(/-?\d+/).map(&:to_i).reduce(0, &:+)
# part 2
def sum_of_numbers(o)
case o
when Array
o.map do |e|
sum_of_numbers(e)
end.reduce(0, &:+)
when Hash
if o.values.include?("red")
0
else
sum_of_numbers(o.values)
end
when Numeric
o
else
0
end
end
h = JSON.load(File.read("input"))
puts sum_of_numbers(h)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment