I'm trying to do the 4th Advent of Code problem using J, and I've ran into lots of problems, but now I have a code that looks like this:
fn =. < 'D:/PyCharm/AOC/output4.txt'
data =. freads fn
concatIntegers =. ,&'x'@,&.":
separado =. ;: data
lista =. > ((i.(#separado)%2)*2) {separado
n_lista =. #lista
n_2 =. n_lista % 4
lista_2 =. concatIntegers each/|:lista
matriz =. 250 4 $ > lista_2
loop =: monad : 0
res =. 0
i=.0
condicion=.0
for_i. i.250 do.
fila =. i { matriz
elfo11 =. 0 {fila
elfo12 =. 1 {fila
elfo21 =. 2 {fila
elfo22 =. 3 {fila
condicion =. ((elfo11 <: elfo21) *. (elfo12 >: elfo22)) +. ((elfo21 <: elfo11) *. (elfo22 >: elfo12))
if. condicion do.
res =. >: res
end.
end.
res
)
loop matriz
What this should do is: Loads a txt file, parses it, creates a matrix, and then using the verb loop
it would add 1 to a counter every time the condition is applied.
The thing is, I can't make that loop work, every time I try running it, it gives me the same error:
|noun result was required: loop
| condicion
|[-30] d:\pycharm\aoc\day4.ijs
I am losing my mind
The code works until it reaches the loop verb I created, but I've been looking through documentation for ages and I can't spot my error
The code until that works as intended
Problem #1: variable i used in:
fila =. i { matriz
is not defined and is considered as unknown verb not noun.
Problem #2: loop iterates on martiz
which is of lenght 250 elements (each element is list of 4 integers). But it does 1000 iterations, so there is out of array bound here.
Try to replace the line:
for. i.1000 do.
by the line
for_i. i.250 do.
Problem #3: there is no priorities for operators, so condition
should be computed as (I guess here):
condicion =. ((elfo11 <: elfo21) *. (elfo12 >: elfo22)) +. ((elfo21 <: elfo11) *. (elfo22 >: elfo12))
Problem #4: res
increment is not saved, try to replace the line:
>: res
by the line
res=. >: res
Problem #5: loop
verb cannot see martiz
noun since that is local, try to replace the line:
matriz =. 250 4 $ > lista_2
by the line
matriz =: 250 4 $ > lista_2