Yes, a fold with early termination would be faster for this year's aoc day 6.
But time was not a bottleneck resource for day 6. And... I'm not going to worry about spoilering aoc puzzles -- there's an entire reddit forum dedicated to sharing solutions and people routinely post their solutions. Anyways, an early terminating fold would be significantly more verbose (and, thus, longer to write) than something like loc=: {{m+m i.~m#@~.\]}} But perhaps converting that to use Z: and F:: would be a fruitful exercise in optimization? -- Raul On Thu, Dec 8, 2022 at 11:28 AM 'Michael Day' via Programming <programm...@jsoftware.com> wrote: > > Having remarked recently (re Advent of Code day 6): > > Very easy today, in J at least, and probably in APL & K/Q. (Though it > suggests one should learn the new fold features, as it's inefficient to > examine all the data for something that might occur early. No problem > with 4kb, but ....) > > I've just had a look at the Jwiki entries about Fold, and wonder how it > would work > for day 6. > > The problem is so easy for J-ers that I don't think this will spoil it > for forum users: > ex > mjqjpqmgbljsphdztnvjfqwrcgsmlb > 4 + 4 i.~ 4 #@~.\ ex > 7 > > QED > > #data NB. the size of my data set > 4096 > ts'4 + 4 i.~ 4 #@~.\ data' NB. time & space > 0.0008555 67232 > > I didn't bother making a function for this! > > One way to stop early, rather than process the whole array, is this: > ts'4 + (# - #@:((}.`])@.(4&([ = #@:~.@:{.))^:_)) data' > 0.0007299 4736 > > (I know it's ugly, and non-optimal!) > > Similar time but with ca 14x space-saving. > > But there must be some sort of Fold to progressively examine 4-windows, > or 14-windows in part 2, until the first appropriate window is found, > when > it stops. That should save time as well as space. Not necessary > here, of > course, but if there was a real application with many megabytes of data... > > Cheers, > > Mike > > > > On 06/12/2022 19:46, Brian Schott wrote: > > I have successfully solved Day 5 using the following looping verb `tomove` > > but cannot craft a Fold version, and would like help. > > > > *[FYI I have included an attachment that seems to load and execute > > properly, but I could NOT get the email versions to load correctly, > > presumably because of some funny characters.]* > > > > tomove=: dyad define > > while. #y do. > > x=.x move {. y > > y =. }.y > > end. > > x > > ) > > > > > > NB. The verb `move` for part 1 is very simple: > > > > move =: dyad define > > 'n f t'=. _1 1 1*y - 0 1 1 > > take =. |.n{.>f{x > > left =. <n}.>f{x > > put =. <take,~>t{x > > (left,put) (f,t)}x > > ) > > > > NB. the inputs are produced as follows. > > > > top =: 0 :0 > > [D] > > [N] [C] > > [Z] [M] [P] > > 1 2 3 > > ) > > > > state=.deb each ;/ |:|.}:1 5 9&{;._2 top > > > > bottom =: 0 :0 > > move 1 from 2 to 1 > > move 3 from 1 to 3 > > move 2 from 2 to 1 > > move 1 from 1 to 2 > > ) > > alpha =. a. {~97+i.26 > > moves =. ".-.&alpha ;._2 bottom > > > > NB. finally the result is produced by the next phrase > > > > {:every state tomove moves > > > > > > ---------------------------------------------------------------------- > > For information about J forums seehttp://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