Hello again.

I managed to solve day 3 and 4, so maybe when I clean up my solutions I
will ask to review them. But right now I have a problem and some questions
on the first part of day 5.

Main things I need in bold, mainly the last one. Feel free to code review
this message as much or as little as you want to.

I can read and parse both the stacks and the instructions without problem,
but I have to manually choose where to separate them.

]p=:1!:1<'/home/deck/projects/adventofcode/2022/d05t1.input'

[D]

[N] [C]

[Z] [M] [P]

1 2 3


move 1 from 2 to 1

move 3 from 1 to 3

move 2 from 2 to 1

move 1 from 1 to 2


]lines=.|:>4{.cutopen p

[[

NZ1

]]

[[[

DCM2

]]]

[

P3

]

]textstacks=:(4,:1)<;.3 }.lines

┌────┬────┬────┐

│ NZ1│DCM2│ P3│

└────┴────┴────┘

]wo=:}:each dltb each ,each textstacks

┌──┬───┬─┐

│NZ│DCM│P│

└──┴───┴─┘

]pc=.cutopen toJ p

┌───────────┬───────────┬───────────┬───────────┬──────────────────┬──────────────────┬──────────────────┬──────────────────┐

│ [D] │[N] [C] │[Z] [M] [P]│ 1 2 3 │move 1 from 2 to 1│move 3 from 1 to
3│move 2 from 2 to 1│move 1 from 1 to 2│

└───────────┴───────────┴───────────┴───────────┴──────────────────┴──────────────────┴──────────────────┴──────────────────┘

ins=: [: ". 'movefrt' -.~ ]

]moves =: ins each 4}. pc

┌─────┬─────┬─────┬─────┐

│1 2 1│3 1 3│2 2 1│1 1 2│

└─────┴─────┴─────┴─────┘

So I can separate the data, but I don't know how to find the line where to
separate them. I tried some things but to no avail. Some variations like
this one.

   'm' i.&1@: sta pc
1


*So finding the first box that starts with 'm', or the first that is only
numbers would help. How could I find this kind of things?*

Now onto the hard part, how to iterate through the whole array of moves.

I can process the moves like this:
move=: 4 : 0

from=:(1{y)-1

to=:(2{y)-1

times=:0{y

m2=:x

for. i.times do.

m1=:(<(({.>from{m2),>to{m2))to}m2

m2=:(<}.>from{m1)from}m1

end.

m2

)

wo move 1 1 2

┌─┬────┬─┐

│Z│NDCM│P│

└─┴────┴─┘

wo move 1 2 1

┌───┬──┬─┐

│DNZ│CM│P│

└───┴──┴─┘

From here *I would like to change the for. but I don't trust myself to do
it well using the power conjunction*. And it brings me up to the last part
that I have problems with, applying this to the whole array of moves.

I tried the \. \ /\. and /\ constructs, but none have worked for me.

   1 (ww&move@>)\moves
┌────┬────┬────┐
│DNZ │CM  │P   │
└────┴────┴────┘

┌────┬────┬────┐
│    │DCM │ ZNP│
└────┴────┴────┘

┌────┬────┬────┐
│CDNZ│M   │P   │
└────┴────┴────┘

┌────┬────┬────┐
│Z   │NDCM│P   │
└────┴────┴────┘

   1 (ww&move@>)/\moves
┌─────┬─────┬─────┬─────┐
│1 2 1│3 1 3│2 2 1│1 1 2│
└─────┴─────┴─────┴─────┘

And some more variations. *How do I execute the same function, with one
parameter being the result from the previous computation (except on the 1st
step)?*

Thanks in advance.
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to