Created
December 12, 2015 13:06
-
-
Save wconrad/34d3ffebbdb8d8537fbc to your computer and use it in GitHub Desktop.
Advent of Code, day 12
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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