Skip to main content

Search

Items tagged with: adventofcode


I know i've already posted about the #adventOfCode for 2022. But hell does it make you feel good when you finish a task. If you are able and you haven't already, start it, it is fantastic!
adventofcode.com
#tech #technology #programming #advent #christmas #christmas2022


Since @tfb asked, I'll explain the code. I know the question was about day 1, but I'll do day 2 first because the code is simpler and uses fewer special tricks in the language.

The first this to remember about KAP syntax is that for function calls, the parser binds to the right. It also evaluates its arguments from right to left.

What this means is that the following code: io:println 1 + math:sin 2` is evaluated like this:

a0 ← math:sin 2<br>a1 ← 1 + a0<br>io:println a1<br>

Another important fact is that KAP functions are either monadic (they accept a single argument to the right of the function name) or dyadic (accepts two arguments, one on each side of the function). Some functions can be called both monadically and dyadically. One such example is - which subtracts when called dyadically but negates when called monadically.

With that in mind, let's take a look at the solution:

+/ 4 8 3 1 5 9 7 2 6 ⊇⍨ 3 (⊥⍤1) 65 88 -[1]⍨ unicode:toCodepoints 1 0 1 ⫽ ⊃ io:read "testdata.txt"<br>

The input data is in the file testdata.txt, and the first thing that happens is that the entire file is read into an array of strings, where each element is one line from the file.

Each line is three characters, so we use the function which converts an array of arrays to a two-dimensional array of characters.

The next part is 1 0 1 ⫽. This function takes an array on the right, and uses the right argument (1 0 1) as a selector. I means to take one copy of the first column, zero copies of the second and 1 copy of the third column. In other words, this drops the centre column.

Starting with the example array we then get this:

    1 0 1 ⫽ ⊃ "A Y" "B X" "C Z"<br>┏━━━━━┓<br>┃<span class="h-card"><a href="https://functional.cafe/@[url=https://functional.cafe/users/a]a[/url]" class="u-url mention">@<span>a</span></a></span> @Y┃<br>┃@B @X┃<br>┃@C @Z┃<br>┗━━━━━┛<br>

Example

We then call the function unicode:toCodepoints which converts the characters to the corresponding Unicode codepoints.

We then want to convert the codepoints to actual numbers from 0 to 2. This is done by subtracting 65 from the first column and 88 from the second. Naturally we use the - function to do this, but in order to avoid having to wrap the entire expression in parenthesis, we use the operator which reverses the arguments, putting value to be subtracted from on the right instead of the left.

(continued)

#kap #apl #AdventOfCode


I've just finished the tasks for day3 of the 2022 #adventOfCode! If you are able, make sure to catch up!
adventOfCode.com


I'm very excited to have started work on the #adventOfCode for 2022!
adventofcode.com
I've completed days 1 and 2. And for my late night brain it hurts, but that's the fun of it, I think...


I'm posting the #adventOfCode, well, code on github. The code is not by far in any shape or form officient but i thought it would be nice to have up there for a bit of fun.
github.com/blindLightning/adve….