mine:

part1 was straightforward - find key/value from the input in the
ticker. If all 3 key/values from the row match the ticker, then it's a
matching row
part2 was a bit trickier - I added on the operator to the input (e.g.
<5 for goldfish) then found the matching row in the ticker and
evaluated the operand/value against the ticker value. If all 3
matched, then it was a match

input =: ;: each cutLF freads 'c:/joe/lang-lab/j/advent2015/day16.txt'

ticker=: ;: every cutLF (0 : 0)
children: 3
cats: 7
samoyeds: 2
pomeranians: 3
akitas: 0
vizslas: 0
goldfish: 5
trees: 3
cars: 2
perfumes: 1
)

smoutput pt1=: (#~ (3 = +/@(ticker e.~ _2[\(2,3,5,6,8,9)&{)) every) input

ticker=: ;: every cutLF (0 : 0)
children: =3
cats: >7
samoyeds: =2
pomeranians: <3
akitas: =0
vizslas: =0
goldfish: <5
trees: >3
cars: =2
perfumes: =1
)

evalPt2=:((3,6,9)&{ ([: ".@;"1 [ ,. {&(}."1 ticker)@]) (0 {"1 ticker)
i. (2,5,8)&{)
smoutput answerPt2=: (#~ (3 = +/@evalPt2) every) input



On Wed, Dec 16, 2015 at 7:40 AM, Ryan Eckbo <[email protected]> wrote:
> This one took a while, whew.
>
> load'regex'
> Defaults=:', ', ': _',~ ': _, 'joinstring <;._2 [ 0 : 0
> children
> cats
> perfumes
> samoyeds
> pomeranians
> akitas
> vizslas
> goldfish
> trees
> cars
> )
>
> NB. part 1
> parse=: deb L:0@:(':'&cut each)@:(','&cut)
> Input0=: ('^Sue \d+: ';'') rxrplc fread'16.txt'
> Input=: deb L:0 parse@:(Defaults,~]);._2 Input0  NB. add _ defaults to each
> line
> Ticker=: /:~ parse s=:'children:3, cats:7, samoyeds:2, pomeranians:3,
> akitas:0, vizslas:0, goldfish:5, trees:3, cars:2, perfumes:1'
> BoxMatrix=: /:~@({.each {./. ])"1 Input  NB. real values will always be
> first box of their group
> Matrix=:".every {: every BoxMatrix
> TickerVec=: ". every {: every Ticker
> smoutput >: (i.>./) Matrix (+/ .=) TickerVec
>
> NB. part 2
> NB. dapply from
> http://code.jsoftware.com/wiki/Essays/Distributed_Apply#Dyadic_Case
> dapply=: 1 : 0
>   (i.#y) u@]@.["0 _1 y
> :
>   r=. ,:x [email protected]&{. y
>   for_i. i.&.<:#y do. r=. r,(i{x) [email protected] i{y end.
> )
> ad=: 1 : 'u *. -.@(_ = [)'
> smoutput >: (i.>./) Matrix +/@:(=`=`(>ad)`=`(<ad)`=`(<ad)`=`(>ad)`=
> dapply)"1  TickerVec
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to