I changed those keys to numbers... and cheated a bit with < and >

t=: ;: > cutLF (':,',CR)-.~ fread {:ARGV NB.
|Sue|440|pomeranians|1|samoyeds|9|children|4|

c=: ;: 'children cats samoyeds pomeranians akitas vizslas goldfish trees
cars perfumes' NB. categories
a=: ".@>@{: + (100 * c i. {.) NB. analyze 'name';num

s=: 'children 3 cats 7 samoyeds 2 pomeranians 3 akitas 0 vizslas 0 goldfish
5 trees 3'
p=: a"1 [_2]\ ;: s,' cars 2 perfumes 1' NB. 3 107 202 303 400 500 605 703
802 901

e=: 3 : '*./ p e.~ a"1 [_2]\ 2}.y' NB. all in y (after Sue:N) belongs to p
echo (e"1 # ]) t NB. |Sue|213|children|3|goldfish|5|vizslas|0|

NB. part2: 107 --> >107, 703 --> >703, 303 --> <303, 605 --> <605
p=: 3 108 109 110 111 112 113 114 202 300 301 302 400 500
p=: p,600 601 602 603 604 704 705 706 707 708 709 802 901
echo (e"1 # ]) t NB. |Sue|323|perfumes|1|trees|6|goldfish|0|



On Wed, Dec 16, 2015 at 4:06 PM, Joe Bogner <[email protected]> wrote:

> 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
>
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to