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

Reply via email to