Thank you. My code addresses the question "Given a point in a
parenthesized string, what is the smallest containing string with
balanced parentheses?", solving in parallel for multiple input points.
Henry Rich
On 12/12/2015 11:35 PM, 'Pascal Jasmin' via Programming wrote:
very impressive, Henry. thanks for sharing.
----- Original Message -----
From: Henry Rich <[email protected]>
To: [email protected]
Sent: Saturday, December 12, 2015 10:12 PM
Subject: Re: [Jprogramming] advent of code day 12
Spurning convert/json.
A nonrecursive solution:
NB. advent 12
NB. part 1
i =: <@;:;._2 iraw =. wd 'clippaste'
+/ (0&".@;;.2~ ~:&(<,'-')) (#~ '-0123456789' e.~ {.@>) ;i
NB. part 2
NB. Assign nesting level to each character; make level of } match {
nlevel =: (+/\ + _1&=) class =: 1 _1 0 {~ '{}' i. iraw
NB. Get locations of points of interest: {} or :"red"
points =: nlevel ,.&(((':"red"'&E. +. e.&'{}') iraw)&#) i. # nlevel
NB. Sort to match starting and ending {}; examine each {} block,
NB. and keep the start/end+1 if it contains :"red"
redblocks =: _2 ]\ ; (<@((0&#)`(0 1 + 0 _1&{)@.(2<#));.1~ '{' =
{&iraw) {:"1 /:~ points
NB. Put red strings in sequence; discard red strings that are included
in other red strings
culledred =: (#~ {."1 >: |.!.0@:(>./\)@:({:"1)) /:~ redblocks
NB. Discard the red strings from the input
culledi =: (_2 ,.@:(-~/\)\ 0 , (,culledred) , #iraw) ;@:(<;.0) iraw
NB. Calculate the requested result on the culled input
+/ (0&".@;;.2~ ~:&(<,'-')) (#~ '-0123456789' e.~ {.@>) ;: culledi
Henry Rich
On 12/12/2015 3:47 PM, Joe Bogner wrote:
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
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm