You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
advent-of-code/2015/ruby/day_12.rb

35 lines
36 KiB

9 years ago
require "json"
9 years ago
class String
def sum
0
end
end
class Integer
def sum
self
end
end
class Array
def sum
self.map(&:sum).inject(&:+)
end
end
class Hash
def sum
if self.values.include?("red")
0
else
self.values.map(&:sum).inject(&:+)
end
end
end
puts JSON.load(DATA.read).sum
9 years ago
__END__
[{"a":{"e":{"e":161,"a":"blue","d":{"e":-14,"a":"red","d":{"c":"yellow","a":[-35,0],"b":"orange","d":{"e":70,"a":"green","d":"blue","j":12,"c":69,"h":"orange","b":92,"g":"yellow","f":"green","i":121}},"c":"blue","h":14,"b":46,"g":62,"f":[179]},"j":{"e":133,"c":"violet","a":"orange","b":"blue","d":"violet"},"c":{"e":151,"a":"violet","d":{"e":"red","c":93,"a":135,"g":{"e":43,"c":"green","a":"orange","b":"green","d":54},"b":69,"d":159,"f":2},"c":"green","h":65,"b":{"c":126,"a":106,"b":190,"d":-40},"g":134,"f":"red"},"h":87,"b":[-3,"yellow",50,120],"g":{"e":[138,83,"red"],"c":["yellow",["red",177,98,"blue",179,"blue","violet",23],108,["green",17,-46,3,99],-43,46,"orange","yellow",{"a":192,"b":39},57],"a":"red","b":195,"d":172},"f":97,"i":160},"a":"orange","d":120,"c":61,"h":"red","b":186,"g":{"e":"orange","a":82,"d":{"a":{"e":"green","c":-5,"a":-13,"b":12,"d":"blue","f":-19}},"c":"blue","h":["violet","violet",82,3,"orange","orange",45,"red",198,[81,"red","green","green","yellow",{"c":"orange","a":"blue","b":193},"blue"]],"b":99,"g":{"a":[64,"orange","blue",98],"b":30},"f":"orange","i":[167]},"f":[{"e":"green","c":168,"a":"red","b":173,"d":"blue"},{"e":"violet","c":90,"a":10,"g":{"a":3,"b":195},"b":"orange","d":{"e":143,"c":-26,"a":136,"g":-28,"b":137,"d":"violet","f":[108,70,19,"blue","violet","orange"]},"f":-11},{"e":188,"a":173,"d":135,"j":148,"c":"orange","h":["orange","yellow",108,"violet","violet","blue",188,"yellow","red"],"b":[145,118,"orange","green","green",17,["red",55,86,-20,"green",118,"violet","green"]],"g":{"e":62,"c":"blue","a":"violet","b":-41,"d":5},"f":"green","i":[33,"orange"]},{"a":"green"},{"a":[87,"violet"],"b":"violet"},{"e":"violet","a":147,"d":"blue","c":["blue",91,"violet","violet","yellow","orange","yellow",-4],"h":"yellow","b":19,"g":{"a":"red"},"f":[-38,"yellow","violet",-10,"orange",29,"violet",52],"i":"red"},54,[-14,-2,{"e":12,"a":"violet","d":"blue","j":"orange","c":180,"h":{"e":186,"a":"green","d":"violet","c":"orange","h":"yellow","b":"yellow","g":"green","f":"violet"},"b":"green","g":{"e":"green","c":103,"a":119,"b":"blue","d":"violet","f":116},"f":22,"i":"yellow"},["violet","blue","blue","violet",44,"orange"],144],"red",[119,"red",[10,15,115,"orange",55,"yellow",85,135,-12,"orange"],61,{"e":"red","c":"yellow","a":34,"b":"blue","d":"orange","f":"yellow"},{"c":{"c":"yellow","a":0,"b":"violet"},"a":"orange","b":"green","d":-37},-21]],"i":{"e":[{"e":123,"a":106,"d":100,"j":"violet","c":164,"h":["red",120,"orange","violet",54],"b":-22,"g":"violet","f":"orange","i":130}],"c":{"e":"blue","a":[[198,82,2,"orange",151],128,"violet",88],"d":72,"c":"blue","h":194,"b":4,"g":149,"f":"violet"},"a":[["blue",27,"green",174],"orange",50,"orange",["orange","violet",154,167],"red","orange",{"c":49,"a":"violet","b":76,"d":{"c":166,"a":"red","b":10,"d":185}},53,"yellow"],"b":"orange","d":101,"f":-1}},"b":{"e":{"e":["blue",92,-20,"red","yellow",129,99,"yellow",38],"a":98,"d":129,"c":-5,"h":"green","b":"violet","g":"blue","f":"yellow","i":"red"},"a":"blue","d":[[20,87,"red",124,-2,"yellow","red",17,"violet","yellow"],{"c":"violet","a":"red","b":161},104,{"e":["blue",[51,47,57,-41,"green",149,-11,"red",103,"orange"],"violet",72,"red",["orange","blue","red"],-8,{"c":"violet","a":68,"b":-43,"d":"green"},"orange"],"a":36,"d":["orange","green","green",106,"green","red","red","red","violet"],"j":{"a":"red","b":144},"c":166,"h":109,"b":[{"a":56,"b":"red"},-41,"blue",36,20,"blue","blue"],"g":188,"f":"yellow","i":-7},[155,["green","green",0,"red","blue"],{"e":144,"c":"green","a":"orange","b":133,"d":10,"f":[91,160,"blue",183,18,43,85,-37]},{"e":"violet","a":-35,"d":57,"c":"violet","h":17,"b":"orange","g":57,"f":"red","i":"blue"},"red","red","green","red",29,{"e":112,"c":["red","blue",68,"red","yellow"],"a":"blue","g":"blue","b":{"a":"red"},"d":162,"f":"green"}],"green",[42,"orange"],[["yellow",183,2,"red",115,"red",-31]]],"j":29,"c":{"e":[{"c":"orange","a":{"e":"orange","c":-47,"a":"blue","b":35,"d":25,"f":"blue"},"b":111,"d":16},-44,-22,179,"yellow",84],"a":{"e":178,"a":["red",183,92],"d":163,"c":105,"h":24,"b":"violet"