This is my (entirely tacit) solution to problem of day 6. it works by exploiting a pair of nice J features:
- ;: for parsing words and it's inverse to piece it together with spaces. - "on","off" and "toggle" defined for changing the lights - "through" for generating lights corresponding to the given indices. - s5parse splits the lines, adds a pair of parentheses, parses words, and rearranges them so each line is formatted as (x1 y1 through x2 y2) action-verb. - Reverse order, so it uses strict right to left precedence, and join into one long phrase. Have fun with the deciphering! NB. Light grid on/off i5 =: freads D,'input6.txt' NB. read inputs NB. After parsing, inverse (J's precedence is right to left), and add the initial light array. s5a =: +/@".@(' 1000 1000$0',~ ,)@|.@s5parse s5parse =: (-2 9 7 6 5 3 1 10)&{&.;:@(,&'()') ;. _2 NB. throw out 'turn' and commas NB. generate coordinates x1 y1 through x2 y2 thr =: 1 : '(i. m) e. m #. [: >@,@{ <@([ + i.@>:@(>.-<.) )"0' NB. make a "through" for 1000 x 1000 arrays through =: 1000 1000 thr on =: >. NB. turn on off =: (<.-.)~ NB. turn off toggle =: ~: NB. toggle. s5b =: +/@,@".@(' 1000 1000$0',~ ,)@|.@s5parse NB. the same as above NB. just change the action verbs. on =: + NB. add off =: 0 >. -~ NB. subtract limit 0 toggle =: (+ +:)~ NB. add double 2015-12-07 20:44 GMT+01:00 Joe Bogner <joebog...@gmail.com>: > Here's my silly version for part 1... We can turn the instructions > into bytecodes and run it through the bytecode interpreter I've been > playing with: > > https://gist.github.com/joebo/92738a1c6df841d53b15 > > I haven't run it through the full 1000x1000 matrix yet. I'm estimating > it would take about 3 hours to run (3000 ops per second) > > smoutput $ compile lines > 27212168 3 > > I could always run it through the OCL version :-) > > > On Sun, Dec 6, 2015 at 11:03 PM, 'Pascal Jasmin' via Programming > <programm...@jsoftware.com> wrote: > > A version focused on reduced typing of parens, uses this file, > > > > > https://github.com/Pascal-J/multiline-tacit-and-macros---J/blob/master/multiline2.ijs > > > > multiline tacit basically inserts parens around each line in a definition > > > > > > Y =: (&{::)(@:]) > > X =: (&{::)(@:[) > > amenD =: 2 : '([ u"_ v{ ])`(v"_)`]} ]' > > reduce =: 1 : '<"_1@[ ([: u &.>/(>@:) ,) <@:]' > > > > coinsert 'multiline' > > > > p3 =: ,@:(<"1)@(0 Tacify) > > > > 4 Y (] + i.@>:@-) 2 Y > > ,.~"0 1 > > 3 Y (] + i.@>:@-) 1 Y) > > > > p2 =: 0 Tacify"1 _ > > (2 + ]) amenD (p3@[) > > ` > >>:@] amenD (p3@[) > > ` > > (0 >. <:@]) amenD (p3@[) > > @. > > 0 X > > ) > > > > b =. (2 , 0 ". every 3 5 7 9 { ])`(1 , 0 ". every 3 5 7 9 { ])@.('on' > -: 2 Y)`(0 , 0 ". every 2 4 6 8 { ])@.(0 Y)"1( ],~ [: < 0:`1:@.('toggle' > -: 0 Y))"1 ;:"1 > cutLF a =. wd 'clippaste' > > > > +/@, ( |. b) p2 reduce 1000 1000 $ 0 > > > > slow because of boxing in reduce > > > > Henry's was quite fast. > > > > > > ----- Original Message ----- > > From: Ric Sherlock <tikk...@gmail.com> > > To: Programming JForum <programm...@jsoftware.com> > > Sent: Sunday, December 6, 2015 10:14 PM > > Subject: Re: [Jprogramming] adventofcode 6 > > > > I struggled with an efficient tacit implementation too and went with a > for > > loop to sequentially update the array. > > I love Henry's take on the problem! > > > > parseInstructs=: 3 :0 > > > > tmp=. y rplc 'turn ';'';'through ';'' > > > > action=. <@(' '&taketo);._2 tmp > > > > coords=. ". ' '&takeafter;._2 tmp > > > > action=. ('off';'on';'toggle') i. action > > > > action ,. coords > > > > ) > > > > > > getIdx=: [: < ([ + i.@>:@-~)&.>/@(_2 ]\ ]) > > > > on=: 1:`(getIdx@[)`]} > > > > off=: 0:`(getIdx@[)`]} > > > > toggle=: ([: -. getIdx@[ { ])`(getIdx@[)`]} > > > > > > applyActions=: 4 :0 > > > > actions=. {."1 x > > > > coords=. }."1 x > > > > for_action. actions do. > > > > coord=. action_index { coords > > > > y=. coord off`on`toggle@.action"_ y > > > > end. > > > > ) > > > > > > Lights=: 1000 1000 $ 0 > > > > Instructions=: parseInstructs freads '~temp/advent6_input.txt' > > > > Lights=: Instructions applyActions Lights > > > > echo +/ , Lights > > > > > > Part 2: > > on=: (1 + getIdx@[ { ])`(getIdx@[)`]} > > > > off=: (0 >. _1 + getIdx@[ { ])`(getIdx@[)`]} > > > > toggle=: (2 + getIdx@[ { ])`(getIdx@[)`]} > > > > Lights=: 1000 1000 $ 0 > > > > Instructions=: parseInstructs freads '~temp/advent6_input.txt' > > > > Lights=: Instructions applyActions Lights > > > > echo +/ , Lights > > > > > > On Mon, Dec 7, 2015 at 1:46 PM, Ryan Eckbo <ec...@cim.mcgill.ca> wrote: > > > >> Wow. > >> > >> These problems are great, I'm learning a lot seeing people's solutions. > >> > >> > >> On 7 Dec 2015, at 10:37, Henry Rich wrote: > >> > >> Part 1: > >>> > >>> i =. <@('y =. y ' , ;@(<@('(' , ,&')');._2)@:(,&' '));._2 wd > 'clippaste' > >>> > >>> through =: (1000 1000 {. ({. ($&1)))/@(-~/\)@:-@(,:~ >:) > >>> > >>> turn =: 1 : 'u' > >>> > >>> '`off on toggle' =: >`+.`~: > >>> > >>> +/@, 3 : i 1000 1000 $ 0 > >>> > >>> > >>> For part 2, use > >>> > >>> '`off on toggle' =: (0 >. -)`+`(+ +:) > >>> > >>> > >>> > >>> > >>> On 12/6/2015 6:03 PM, David Lambert wrote: > >>> > >>>> Ryan made a mask shape 1000 1000 and applied it appropriately to a > >>>> cumulative array. Pascal updated the accumulation array in place. I > did so > >>>> likewise but used the dyadic form of amend with gerund m. Pascal > computed > >>>> the index in one place, which I prefer to my 3 sites. > >>>> > >>>> g > >>>> [: r/ [: _&". {::"0 1 > >>>> > >>>> r NB. absolute value not required > >>>> ([ + i.@:|@:(-/)) >: > >>>> > >>>> > >>>> NB. noun L is the array of lights. > >>>> > >>>> identify NB. whoops! returns a vector > >>>> 0 1 2 #~ 'turn on'&({.@E.) , 'turn off'&({.@E.) , 'toggle'&({.@E.) > >>>> > >>>> on > >>>> 3 : 0 > >>>> I=:([: < 2 6&g ; 4 8&g)@;:y > >>>> NB. L=:I (1:)`([)`(])}L > >>>> L=:I (>:@:{)`([)`(])}L > >>>> EMPTY > >>>> ) > >>>> > >>>> off > >>>> 3 : 0 > >>>> I=:([: < 2 6&g ; 4 8&g)@;:y > >>>> NB.I (0:)`([)`(])}L > >>>> L=:I ((0>.<:)@:{)`([)`(])}L > >>>> EMPTY > >>>> ) > >>>> > >>>> toggle > >>>> 3 : 0 > >>>> I=:([: < (<:2 6)&g ; (<:4 8)&g)@;:y > >>>> NB. L=: I (-.@:{)`([)`(])}L > >>>> L=:I (2+{)`([)`(])}L > >>>> EMPTY > >>>> ) > >>>> > >>>> > >>>> NB. what the heck??? Why is this zero? > >>>> > >>>> +/, L [ on`off`toggle@.({.@identify)@,;._2(1!:1)<'/tmp/a' [ L=:1000 > >>>> 1000$0 > >>>> 0 > >>>> +/,L > >>>> 14110788 > >>>> > >>>> > >>>> > >>>> > >>>> Date: Mon, 07 Dec 2015 08:46:59 +1100 > >>>>> From: "Ryan Eckbo"<ec...@cim.mcgill.ca> > >>>>> To: programming<programm...@jsoftware.com> > >>>>> Subject: Re: [Jprogramming] adventofcode 6 > >>>>> Message-ID:<b4007f86-91ad-409c-95d6-ba6d6966c...@cim.mcgill.ca> > >>>>> Content-Type: text/plain; format=flowed > >>>>> > >>>>> How do your on/off/toggle functions work? My solution also updates a > >>>>> global matrix, > >>>>> but my implementation doesn't feel good. Looking forward to seeing > >>>>> nicer solutions. > >>>>> > >>>>> xind=: 3 : 0&{ + [: i. >:@:(-/)@:(2 0&{) > >>>>> yind=: 3 : 1&{ + [: i. >:@:(-/)@:(3 1&{) > >>>>> mkmask=: 3 : '1 inds} 1000 1000 $ 0 [ inds=: < (xind y);(yind y)' > >>>>> on=: 3 : 'lights=: lights + mkmask y' > >>>>> off=: 3 : 'lights=: 0 >. lights - mkmask y' > >>>>> toggle=: 3 : 'lights=: lights + +: mkmask y' > >>>>> instructions=: <;._2 (' through ';',';'turn ';'') stringreplace > >>>>> fread'/tmp/input.txt' > >>>>> > >>>>> lights=: 1000 1000$0 > >>>>> do each instructions > >>>>> +/, lights > >>>>> > >>>>> > >>>> ---------------------------------------------------------------------- > >>>> 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 > > ---------------------------------------------------------------------- > > 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