The OCL version of the bytecode interpreter completes instantly and
provided the right answer

warning: ugly code (no attempt to make nice)

https://gist.github.com/joebo/1a9fbb9c2d499209feba



On Mon, Dec 7, 2015 at 2:44 PM, Joe Bogner <joebog...@gmail.com> wrote:
> 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

Reply via email to