Search
Items tagged with: adventofcode
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
adventOfCode.com
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...
github.com/blindLightning/adve….
GitHub - blindLightning/advent-of-code-2022: Here is my messy, messy code for the 2022 advent of code.
Here is my messy, messy code for the 2022 advent of code. - GitHub - blindLightning/advent-of-code-2022: Here is my messy, messy code for the 2022 advent of code.GitHub