I didn't follow this in detail, but I do know that more examples are
needed, and hope you will add this one to the NuVoc page, or hive it off
to a new FoldExamples page. The pattern you end up with looks generally
useful.
Henry Rich
On 12/22/2021 9:08 AM, Raul Miller wrote:
previous message: http://jsoftware.com/pipermail/chat/2021-December/009000.html
AoC day 2 details: https://adventofcode.com/2021/day/2
For day 2, we were "piloting a submarine" given a sequence of steering
instructions,
My approach was to convert those directions to numeric form:
normalize=: _&".@rplc&(;:'forward 1 0 down 0 1 up 0 _1')
sample=: normalize;._2 {{)n
forward 5
down 5
forward 8
up 3
down 8
forward 2
}}
The result looks like this:
sample
1 0 5
0 1 5
1 0 8
0 _1 3
0 1 8
1 0 2
Thus, for the first part, I finished converting this to vector form,
summed the vectors and then found their product (since the puzzle asks
us to multiply the final coordinates):
aoc2a=: {{
*/+/(({:"1)* }:"1) y
}}
For the second part, we had to interpret things a bit differently. We
had an 'aim' value that we multiplied into our depth calculation,
along with moving us horizontally.
I decided to use this as an opportunity to get acquainted with J's
fold (fcap) conjunction. Reading the nuvoc page, I was frustrated by
the lack of orientation materials. I was having to skip all over the
page to figure out what's what.
So I added some basic definitions to the page. Hopefully someone
besides myself finds them helpful:
https://code.jsoftware.com/wiki/Vocabulary/fcap#Definitions
Anyways, my initial implementation looked like this:
aoc2bv=: {{
'dH0 dA0 N'=. x
'A H D'=. y
dA=. N*dA0 NB. change (difference) in aim
dH=. N*dH0 NB. horizontal change
dD=. A*dH NB. depth change
y+dA,dH,dD
}}
Here, y is the accumulating value(s), and I will be dropping the first
value (dA) from the final result.
aoc2b=: {{*/}. 0 0 0&(]F..aoc2bv) y}}
Just for fun, I went back and implemented a fully tacit variant:
aoc2BV=: ([ + ((] , {.@[ * {:@]) ({: * |.@}:)))~
aoc2B=: {{*/}. 0 0 0&(]F..aoc2BV) y}}
aoc2b sample
900
aoc2B sample
900
This particular fully tacit form doesn't seem to add anything -- it's
mostly positional manipulation, and I think I prefer variable names
for that. But reading back on this, I am now thinking that something
like this might make more sense:
b2fold=: 0 0 0&(]F..{{
'dH0 dA0 N'=. x
'A H D'=. y
dA=: N*dA0
dH=: N*dH0
dD=: A*dH
y+dA,dH,dD
}})
b2=: {{*/}. b2fold y}}
b2 sample
900
This approach still requires the reader to understand fcap. But it
brings the pieces of that part of the implementation together.
I hope this makes sense to someone...
--
This email has been checked for viruses by AVG.
https://www.avg.com
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm