Can't sleep. So here's last year's Advent of Code, day 1, in 3 lines of J.

Simple, right?

Alright, not so simple. I have annotated it in the second picture.

I'm sure you can do this even more tersely, but I'm very much a newbie to J.

Looking forward to trying it on this year's problems. 😄

Finally figured out how to write a sliding window function in J.

Code's now at https://gist.github.com/SamirTalwar/a0dddc670bda4a5cc757574bdeb5abde. I'm not uploading it to my main repo because it's not really set up to handle more than one language for the same year.

Solutions for Advent of Code 2021, in J. This is a warmup for 2022.

Solutions for Advent of Code 2021, in J. This is a warmup for 2022. - AOC_01_1.ijs

Gist
BTW, the biggest challenge was `(x, 1)`. I was converting constants to variables, so I tried replacing `3 1 $ i. 3` with `x 1 $ i. x`. The problem is that `3 1` is an array and `x 1` is really `(x) 1`, which doesn't parse. I had to explicitly concatenate `x` and `1` with the comma (`,`) operator.

`$` is the "reshape" operator, which you can use to reshape an array.

`i. n` creates a range from 0 to n.

It works like this:

```
3 3 $ i. 9
0 1 2
3 4 5
6 7 8
```

There's gotta be a better way to create a column of numbers from 0 to n than `(n , 1) $ i. n`, but I can't find it.

Advent of Code 2021, day 2, in J.

I do not like this solution. It relies a lot on mutation and just pretending the input is J (`".` is basically `eval`).

I need to find a way to fold over my input better. Tried looking into state machines but it made my brain bleed. (https://www.jsoftware.com/help/dictionary/d332.htm)

If you need alt text, you're probably better off viewing the code at https://gist.github.com/SamirTalwar/a0dddc670bda4a5cc757574bdeb5abde.

;: Words - Sequential Machine

I made my first J adverb! It takes a dyadic verb (a verb with two parameters, to its left and right), and makes the right parameter a list, which it folds over.

I feel very smug. Better go to bed before this gets out of hand.

Code here (AOC_02_2.ijs): https://gist.github.com/SamirTalwar/a0dddc670bda4a5cc757574bdeb5abde

Solutions for Advent of Code 2021, in J. This is a warmup for 2022.

Solutions for Advent of Code 2021, in J. This is a warmup for 2022. - AOC_01_1.ijs

Gist
I'm still trying to wrap my head around the idea that J functions ("verbs") can only take 1 or 2 parameters, and that you have to compose them cleverly in order to get more.