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