Hi Raul, your version is indeed a very nice simplification of my amend
cludge.
Also a nice job on the parsing of the input.


On Tue, Jan 4, 2022, 02:49 Raul Miller <[email protected]> wrote:

> Because I could have expressed this clearer, here's a complete
> implementation of day 13, based on Jan-Pieter Jacobs' approach:
>
> prep=: {{
>    FOLD=.ALONG=.]
>    X=.|.Y=.0 1
>    <@(".;._2);._2 toupper rplc&('=';'*';(LF,LF);LF,'/') y,'/'
> }}
>
> fold=: [ ~.@:<. |@(-~ +:)&.|:
>
> a13=: [: # (fold {.)&>/
>
> b13=: ' #'{~ ((i.@] e."1 #.~) 1+>./)F..(fold~)&>/
>
> Using the noun 'sample' defined in
> http://www.jsoftware.com/pipermail/programming/2022-January/059525.html
>
>    a13 prep sample
> 17
>    b13 prep sample
> #####
> #   #
> #   #
> #   #
> #####
>
>
> (Still looks better in a monospaced font, though...)
>
> Anyways, it's almost like this one was designed to illustrate J.
>
> --
> Raul
>
> On Mon, Jan 3, 2022 at 2:48 PM Raul Miller <[email protected]> wrote:
> >
> > Hmm... I thought I had had trouble using a bitvector left argument to
> > u;.k (with k in _2 _1 1 2). I am glad to see that that actually works.
> > I'll have to use that more in the future.
> >
> > (And, I noticed, while reading over my code, that I had left a line in
> > my a13 which was nonsense because it used an earlier naming
> > convention. Fortunately, it's an innocuous problem. But it's still not
> > good.)
> >
> > C'est la vie...
> >
> > Anyways, I like the idea of doing the folds on the coordinate pairs
> > directly. It was easy for me to visualize what was happening the other
> > way, but your way is slick.
> >
> > That said, introducing a slight representation change (in pfold), you
> > could have done this for fold:
> >
> > pfold=:(('xy'=11&{) * ".@(13&}.));._2@}.@:>@{:
> >
> > fold=: [ ~.@:<. |@(-~ +:)&.|:
> >
> > Which is ... nice (in my opinion).
> >
> > Thanks,
> >
> >
> > --
> > Raul
> >
> >
> > On Mon, Jan 3, 2022 at 2:06 PM Jan-Pieter Jacobs
> > <[email protected]> wrote:
> > >
> > > Day 13 was sort of involved on the parsing part, and relatively easy
> on the
> > > actual solution.
> > > I kept the dots in the coordinate domain, as it seemed less heavy
> handed
> > > than converting it to "paper", the transform is also very easy in the
> > > coordinate domain.
> > > I used the dots as is, and encoded the folds as axis, fold_position
> pairs.
> > > The append in singlefold updates the row (singlefold operates under
> &.|:
> > > such that x is the first row, and y is the second row) corresponding
> to the
> > > axis in the fold spec, where newrow calulates that row, performing the
> > > actual fold. According to the spec, the fold line itself disappears,
> done
> > > by remfoldline.
> > >
> > > NB. process first part to array of dot coords
> > > pdots=: (".;._2)@>@{.
> > > NB. process folds, turning it in ints axis position pairs
> > > pfold=:(('xy'i.11&{) , ".@(13&}.));._2@}.@:>@{: NB. folds
> > > NB. Joint process verb for dots and folds
> > > prep=: (pdots ; pfold)@(<;.2~ (2#LF)&E.)@,&LF NB. parse two fields
> > >
> > > i13=: prep in 13 NB. dots ; folds as (axis, index) pairs
> > >
> > > NB. A: how many dots after first fold; Dots treated as coordinate pars
> > > NB. calculate folded row x: |:dots; y:|:fold spec
> > > NB.      index  mirror   dots@axis
> > > newrow=: ({:@] ([-|@:-) ({~ {.))
> > > NB. Perform a single fold: x:dots; y:fold spec
> > > NB. where,what,into:  newdots, axis,dots  under transpose, to make
> > > dimension firstaxis in both dots and folds
> > > singlefold=:         (newrow`({.@])`[}    &.|:)
> > > NB. remove the fold line (left dots, right fold)
> > > remfoldline=: ([ #~ ({"1~ {.@]) ~: {:@])
> > > NB. perform a fold (x: dots; y: fold spec), ~. because folding will
> overlap
> > > some dots, which should be counted only once.
> > > fold=: singlefold ~.@remfoldline ]
> > >
> > > Part a performs the fold in the first fold spec, and counts the dots
> > > surviving the ~. in fold.
> > > a13=:[: # (fold {.)&>/
> > >
> > > Part b does all folds and find 8 capitals; using F.. requires fold
> specs to
> > > be right hence it requires ~ .
> > >
> > > NB.  draw draws the dots on a paper of a fitting size: x:dot coords;
> > > seemingly, requires |: for not having vertical, mirrored text.
> > >
> > > draw=: [: |: ('#'"_)`]`(' '$~ >:@(>./)@])}~
> > > NB. do successive folds, then draw
> > > b13=: (draw F.. (fold~))&>/
> > > (a13;b13)i13
> > >
> > > Likely here and there, there could be some simplifications, e.g.
> removing
> > > the &.|: in singlefold and reversing arguments to be used with F..,
> but the
> > > above worked satisfactorilly.
> > >
> > > Just as a note, my solutions might be complicated, but I was not
> solving
> > > the problems in real-time ; actually, now I'm stuck on part B of day
> 15, so
> > > in two days, I won't be able to follow this tread (in real-time)
> anymore
> > > :/. No worries, I'll post my solutions when I get to solve the
> problems.
> > >
> > > Jan-Pieter
> > >
> > >
> > > On Mon, Jan 3, 2022, 03:43 Raul Miller <[email protected]> wrote:
> > >
> > > > https://adventofcode.com/2021/day/13
> > > >
> > > > We are now past the half way point for these puzzles.
> > > >
> > > > For day 13, our puzzle was about getting the activation code (for a
> > > > thermal camera).
> > > >
> > > > Example data looked like this:
> > > >
> > > > sample=:{{)n
> > > > 6,10
> > > > 0,14
> > > > 9,10
> > > > 0,3
> > > > 10,4
> > > > 4,11
> > > > 6,0
> > > > 6,12
> > > > 4,1
> > > > 0,13
> > > > 10,12
> > > > 3,4
> > > > 3,0
> > > > 8,4
> > > > 1,10
> > > > 2,14
> > > > 8,10
> > > > 9,0
> > > >
> > > > fold along y=7
> > > > fold along x=5
> > > > }}
> > > >
> > > > The numbers which look like coordinate pairs were coordinate pairs --
> > > > each marking a pixel on a sheet of paper. However, the coordinate
> > > > pairs are "x,y" coordinates, so the first number in the pair selects
> > > > the column and the second number selects the row.
> > > >
> > > > The "fold along" instructions were instructions on how to fold that
> paper.
> > > >
> > > > So, the first step would be to convert the data to a list of fold
> > > > instructions and a representation of that piece of paper:
> > > >
> > > > unpack=:{{
> > > >    k=. 1+I.(LF,LF) E. y
> > > >    dims=.1+>./pairs=. _&".;._2 rplc&', ' k {. y
> > > >    folds=: 2 4 (;".)&>/@:{"1 ;:;._2 (k+1)}. y
> > > >    folds;|: (i.dims) e."1 dims#.pairs
> > > > }}
> > > >
> > > > Here, the steps were:
> > > >
> > > > (1) Find the index of the blank line.
> > > > (2) coordinate pairs precede that line, and the dimensions of the
> > > > "piece of paper" would be 1 greater than the largest coordinate along
> > > > each dimension
> > > > (3) For the folds, we tokenize each line, extract the relevant tokens
> > > > (and convert the numbers from literal to integer).
> > > > (4) Finally, we package up the results. Transpose (|:) fixes the
> > > > row/column vs x,y issue.
> > > >
> > > > S=: unpack sample
> > > >
> > > > For the first part all we had to do was figure out how to perform the
> > > > first fold (and count the lit pixels).
> > > >
> > > > The rules for folding were that the axis we were folding on would be
> > > > empty (no lit pixels), and when lit pixels overlap they merged to
> > > > become a single pixel. In other words, we combine pixels using +. (or
> > > > >.).
> > > >
> > > > The only real "trick" here is recognizing that the folded half is not
> > > > only reversed but right (or bottom) aligned rather than the usual J
> > > > alignment rule for argument cells.
> > > >
> > > > (Also, I implemented X folds using transpose and the Y fold
> > > > implementation.)
> > > >
> > > > fold=:{{
> > > >   'label axis'=. y
> > > >   if. 'x'=label do.flip=. |: else. flip=. ] end.
> > > >   paper=. flip x
> > > >   assert. 0=+/axis { paper
> > > >   flip (axis{.paper) merge |.(1+axis)}.paper
> > > > }}
> > > >
> > > > merge=:{{
> > > >   shape=. x (>. * _1 ^ >&{.)&$ y
> > > >   x +.&(shape&{.) y
> > > > }}
> > > >
> > > > With our fold operation defined, part a was trivial:
> > > >
> > > > a13=:{{
> > > >   'folds paper'=. y
> > > >   pairs=. |."1 pairs
> > > >   +/,paper fold {.folds
> > > > }}
> > > >    a13 S
> > > > 17
> > > >
> > > > (Also be aware that 'fold' is a verb while 'folds' is a noun.)
> > > >
> > > > For part b, we were supposed to perform the entire sequence of folds,
> > > > and then read the text represented by the resulting pixels. For the
> > > > sample data, this was just a square (or a lowercase 'o').
> > > >
> > > > Of course, for style points we ought to use a fold conjunction here
> > > > (you can tell someone was having maybe just a bit too much fun
> putting
> > > > together these puzzles):
> > > >
> > > > b13=: {{
> > > >   'folds paper'=. y
> > > >   ' #' {~  paper ]F..(fold~) folds
> > > > }}
> > > >
> > > > Sadly, my argument order was the opposite of what J's fold
> conjunction
> > > > needs, but that's simple enough to compensate for. (But ... this
> could
> > > > have been more concise.)
> > > >
> > > >    b13 S
> > > > #####
> > > > #   #
> > > > #   #
> > > > #   #
> > > > #####
> > > >
> > > >
> > > > (It looks better in a mono-spaced font.)
> > > >
> > > > --
> > > > Raul
> > > >
> ----------------------------------------------------------------------
> > > > 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