somewhat tricky as I hadn't done recursion in J prior to this:

require 'convert/json'

input =: fread 'c:/joe/lang-lab/j/advent2015/day12.txt'

json=: dec_json input

traverse=: (".@":@(0&{))`($: each)@.(0 >~ L.)
smoutput pt1=: +/ ; S:0 (traverse json)


hasRed=:(<'red') e.~ [
NB. check for 1 in 2nd row only if it's a rank 2 array
NB. attributes of objects show up in the 2nd row
NB. we have to skip rank 1 arrays because [1,"red",5] is 6
onAttr=: 0:`(*@(+/)@,)@.([: 2&<: #@:$)
traverse=: (".@":@(0&{))`($: each)`(0:)@.( (2:^:(onAttr@:hasRed))^:(])
(0 >~ L.))
smoutput pt2=: +/ ; S:0 (traverse json)
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to