Day 3 Advent of Code in J

108 views
Skip to first unread message

Gustavo Kishima

unread,
Oct 24, 2024, 8:46:16 AM10/24/24
to forum
Hello!

I'm trying to solve this puzzle: https://adventofcode.com/2015/day/3

I had this idea of "mapping" each symbol/arrow to an array of two fields, indicating an xy coordinate to be incremented, so like,
v:  1 0
^: _1 0
<: 0 _1 
>: 0 1

And then I thought of incrementing a sort of counter, which would be the "current" position, while incrementing positions in a large array that would be the houses visited, so, to find the answer, I would just search for all the houses with numbers larger than 0.

But I got stuck on the mapping part. Am I going at the wrong direction here?

I was trying to do a map like this:

```J
d=.2085 2038 $ 0 NB. All houses. From the values of `d`, it's a good idea
      NB. to start at the bottom

NB. Somewhat of ifs
0 1^('>'&=)+1 0^('v'&=)''
1 0^('v'&=)'v'
```
(I saw this solution but I was trying to do an alternative)

Thanks in advance!

Raul Miller

unread,
Oct 24, 2024, 4:50:09 PM10/24/24
to fo...@jsoftware.com
Hmm... I think I'd do it something like this:

parse=: 0,".@rplc&('^>v<';"0 <;.1' 1j0 0j1 _1j0 0j_1')

(I was initially inclined to leave out the unnecessary 'j0' bits, but
I think putting them in makes this slightly easier to read.)

Then the part 1 would examples be:

#~.+/\parse '>'
2
#~.+/\parse '^>v<'
4
#~.+/\parse '^v^v^v^v^v'
2

Depending on part 2, I might later remove the '0,' from parse and make
it be a part of my part1 implementation.

Note that other approaches (like using 1 0, 0 _1, _1 0,:0 1 or
whatever) could easily be made to work, but for simple 2d coordinates,
I prefer using complex numbers. Still, if you wanted to do it that
way, you could go like this:

parse=: 0,".@rplc&('^>v<';"0 <;._1'/1 0/0 1/_1 0/0 _1')"0

I hope this helps,

--
Raul
> To unsubscribe from this group and stop receiving emails from it, send an email to forum+un...@jsoftware.com.

trx2358...@yahoo.com

unread,
Oct 25, 2024, 1:37:35 AM10/25/24
to Digest Recipients
Another option is to look up the direction and then index into the positional delta:

   p=.(((,/-)@=i.2){~'v>^<'i.])
   p'>>v<<^'
 0  1
 0  1
 1  0
 0 _1
 0 _1
_1  0

I.e. Make a “mapping” by simply aligning the two lists.

Michael Day

unread,
Oct 25, 2024, 4:31:24 AM10/25/24
to fo...@jsoftware.com
Not sure if this adds anything,  but it's just earned me a Gold Star!
Taking a leaf from Raul's book,  using complex coords - I usually use 0 0, 0 1 etc

    #~.+/\0, 1j0 0j1 _1j0 0j_1{~ '^>v<' i. fread'~user/aoc1503.txt'
2592

OR - if you prefer not to prepend the origin, 0, do
   1 + #~.+/\  1j0 0j1 _1j0 0j_1{~ '^>v<' i. fread'~user/aoc1503.txt'
2592

Cheers,

Mike
To unsubscribe from this group and stop receiving emails from it, send an email to forum+un...@jsoftware.com.


Virus-free.www.avast.com

Raul Miller

unread,
Oct 25, 2024, 11:42:46 AM10/25/24
to fo...@jsoftware.com
Yes, indexing is better here.

Thanks,

--
Raul

Gustavo Kishima

unread,
Oct 28, 2024, 11:03:37 AM10/28/24
to forum, Raul Miller
Hmm, I think it would be better if I reviewed complex number properly apparently!
Thanks, I'm trying to understand all of this properly!

Tony Poole

unread,
Oct 28, 2024, 4:56:55 PM10/28/24
to fo...@jsoftware.com
I enjoyed this problem and the associated discussion.  My solution was almost identical to Mike's, though I liked Raul's solution for its different approach and use of J verbs I seldom work with.

That said, I have two comments:
  • Though the use of complex numbers is clever, you do pay a relatively high CPU cost:
Given moves contains the four directions, deltasi and deltac contain the coordinate changes corresponding to each move as integer pairs and a complex number respectively, and route contains the sample data, you have

   moves;"0 1 deltai;"1 0 deltac
┌─┬────┬────┐
│>│1 0 │1   │
├─┼────┼────┤
│<│_1 0│_1  │
├─┼────┼────┤
│^│0 1 │0j1 │
├─┼────┼────┤
│v│0 _1│0j_1│
└─┴────┴────┘


   #~.+/\0,deltai{~moves i. route          NB. using integer pairs
2592

   6!:2 '#~.+/\0,deltai{~moves i. route'   NB. CPU using integer pairs
0.000632
  
   #~.+/\0,deltac{~moves i. route          NB. using complex numbers
2592

   6!:2 '#~.+/\0,deltac{~moves i. route'   NB. CPU using complex numbers
0.024576

However, since the actual difference in CPU use is small, use a complex number approach if it clarifies the solution, provides utility, or is just your preference.
  • On Mike's suggestion...  you want to leave in the prepend of 0.  If you take it out, in favor of adding 1 to the final count, you get a wrong house count, 1 more than it should be, when your route visits the starting point exactly two times.  You can see this using a route of "<><"
   #~.+/\0,deltai{~moves i. '<><'          NB. correct
2

   1+#~.+/\deltai{~moves i. '<><'          NB. wrong
3

That's it.  Thanks.

--
"Never put off till tomorrow what may be done day after tomorrow just as well."     Mark Twain

Mike Day

unread,
Oct 29, 2024, 8:14:16 AM10/29/24
to fo...@jsoftware.com
Sorry about the remarks about avoiding prepend.  I should 
have checked my assertion.  Thanks for pointing out the 
problem,

Mike

Sent from my iPad

On 28 Oct 2024, at 20:56, Tony Poole <tqp...@gmail.com> wrote:



Gustavo Kishima

unread,
Nov 25, 2024, 1:23:20 PM11/25/24
to forum, Mike Day
Hello! I have a question about this part of the solution:


parse=: 0,".@rplc&('^>v<';"0 <;._1'/1 0/0 1/_1 0/0 _1')"0

"0 <;._1

It seems that I know that < boxes the noun it's given, but I couldn't figure out what goes on with the other parts of it. Why are they required? What happens in this phrase?
I also know that "0 is so that the verb is applied to rank 0 (I guess?)

Thanks in advance!

LdBeth

unread,
Nov 25, 2024, 6:10:50 PM11/25/24
to fo...@jsoftware.com, Mike Day
>>>>> In <15643b2e-bfa0-44a6...@jsoftware.com>
>>>>> Gustavo Kishima <gustavo...@gmail.com> wrote:

> Hello! I have a question about this part of the solution:

> parse=: 0,".@rplc&('^>v<';"0 <;._1'/1 0/0 1/_1 0/0 _1')"0

> "0 <;._1

> It seems that I know that < boxes the noun it's given, but I couldn't figure out what goes on with
> the other parts of it. Why are they required? What happens in this phrase?
> I also know that "0 is so that the verb is applied to rank 0 (I guess?)

The ('^>v<';"0 <;._1'/1 0/0 1/_1 0/0 _1') is a nonu, and it is

| ('^>v<') (;"0) ((<;._1) ('/1 0/0 1/_1 0/0 _1'))

| nonu dyad (monad nonu)

The phrase <;._1'/1 0/0 1/_1 0/0 _1' is just a fancy way to write

| 1 0;0 1;_1 0;0 _1

and what ;"0 does is concatenate each char in the left string with
the boxes on the right.

so the result is

┌─┬────┐
│^│1 0 │
├─┼────┤
│>│0 1 │
├─┼────┤
│v│_1 0│
├─┼────┤
│<│0 _1│
└─┴────┘

LdBeth

unread,
Nov 25, 2024, 6:25:18 PM11/25/24
to fo...@jsoftware.com, Mike Day
>>>>> In <tencent_8BFF5CDCB6BAE7...@qq.com>
>>>>> LdBeth <and...@foxmail.com> wrote:

>> It seems that I know that < boxes the noun it's given, but I couldn't figure out what goes on with
>> the other parts of it. Why are they required? What happens in this phrase?
>> I also know that "0 is so that the verb is applied to rank 0 (I guess?)

ldb> The ('^>v<';"0 <;._1'/1 0/0 1/_1 0/0 _1') is a nonu, and it is

ldb> | ('^>v<') (;"0) ((<;._1) ('/1 0/0 1/_1 0/0 _1'))

ldb> | nonu dyad (monad nonu)

ldb> The phrase <;._1'/1 0/0 1/_1 0/0 _1' is just a fancy way to write

ldb> | 1 0;0 1;_1 0;0 _1

Actually I forget the string quotes, it should be

'1 0';'0 '1;'_1 0';'0 _1'

Gustavo Kishima

unread,
Nov 26, 2024, 1:51:08 PM11/26/24
to forum, LdBeth, Mike Day

> The phrase <;._1'/1 0/0 1/_1 0/0 _1' is just a fancy way to write
> '1 0';'0 1';'_1 0';'0 _1'

I see, but how does it work? Is there an arcane string processing going on here?

LdBeth

unread,
Nov 26, 2024, 2:17:21 PM11/26/24
to Gustavo Kishima, forum, Mike Day
>>>>> In <4244bf3d-8a1e-4ee4...@jsoftware.com>
>>>>> Gustavo Kishima <gustavo...@gmail.com> wrote:
> [1 <multipart/alternative (7bit)>]
> [1.1 <text/plain; UTF-8 (quoted-printable)>]
> [1.2 <text/html; utf-8 (quoted-printable)>]
>> The phrase <;._1'/1 0/0 1/_1 0/0 _1' is just a fancy way to write
>> '1 0';'0 1';'_1 0';'0 _1'

> I see, but how does it work? Is there an arcane string processing going on here?
> Em segunda-feira, 25 de novembro de 2024 às 20:25:18 UTC-3, LdBeth escreveu:

<;._1 is a verb that splits the array into boxes using the first element
as the 'separator'.

See https://code.jsoftware.com/wiki/Vocabulary/semidot1

So there is no particular reason to use '/'

<;._1';1 0;0 1;_1 0;0 _1' would give same result.

Reply all
Reply to author
Forward
0 new messages