Combining values in branches according to a value, consecutively?

So I’m trying to combine data from a list into chunks, and then adding the values in each chunk into one number.

I’ve been at this for literally six hours now and I haven’t found an answer in any of the 50 topics I’ve read so far. I had hoped to solve it on my own, but I’m stuck and in way over my head.

The idea is to find consecutive values in the first part of each branch, merge them into a list with only one of each consecutive value.

And then add together the values that come second for each value accompanying the first value on each branch.

So for every consecutive occurrence of ‘1’, ‘2’ or ‘3’ on each branch, I want to add together the accompanying numbers, see this screenshot as an example:

I’ve tried Path Mapper, Treefrog plugins (from the Human package), but I keep running into things where things don’t click anymore. Components like delete consecutive help me with the first list, but it does not offer an option to sort and add the values in the second list.

I hope someone knows the answer, it’s been quite a rough ride, I’ve learned a lot by looking at other topics, but I wasn’t able to make definitive solution for myself for this specific problem and I think it could be interesting in general.

To illustrate: I’ve also spent over half an hour finding out how to add three values in three separate branches before discovering I could simply explode the tree and use simple addition to add the values per branch… So I feel like I’m just looking in the wrong spot and thinking too difficult and it’s probably not even that hard in the end.

In this file I have added some explanation and a hand-written example, so I hope my data is complete enough to find a solution:
makingchunks-adding-branches.gh (22.3 KB)

After this is solved I am looking for a way to perform more consecutive operations on this data if I don’t manage to find this out on my own. So if you know of any handy components for consecutive calculations I would be more than happy to know about them. But it will probably come down to either path mapper expressions right?

This is very easy with code (C# for me): just a single LINQ query per branch path. If no good Samaritan provides a native solution I’ll post an indicative demo. But - as you said - if you want “more operations” (and probably quite complex and/or nested etc etc) … code is your best (and maybe the only) friend,

Hi, thanks, I’ve been indeed looking for a native solution as this is part of my quest of doing the ‘Advent of Code 2021’ challenges completely in Grasshopper components (plugins allowed) >>> Advent of Code 2021 (no spoilers please!)

This is why I also put some work in reading about evaluations and expressions. I found a nice list in the Expression Designer, but I wasn’t able to make it work in the end.

I think I could pull it off to make it in Processing, but I would consider that cheating.

If it must be native GH, here is one idea:
makingchunks-adding-branches_v2.gh (24.2 KB)
I feel like there must be a simpler solution.

2 Likes

Hmm, that’s actually a nice workaround using points. It’s basically a ‘physical’ array like this. I like it.

Edit:
I was wrong, you’re right! I’ve taken a break and I now see my mistake…

Basically the logic is:
we have a number c, its default value is 0

every time the previous number doesn’t match with the current number, the value is increased by 1.
At the end we have 638, which tallies with Adam’s script as well.

python code here:

c=0
for n in range(len(v1)):
    if not v1[n]==v1[n-1]:
        c+=1
result=[0]*(c+1)

k=0
result[0] = v2[0]  
for n in range(1,len(v1)):
    if not v1[n]==v1[n-1]:
        k+=1       
    result[k]+=v2[n]

EDIT1:
I just found out my list doesn’t match with yours, from {0;6} to {0,7}.
ummm… maybe i misinterpreted your logic wrongly…

EDIT2:
I believe you made a mistake there: for {0;6}
it should pull an item from the list three times. Is it not true?
makingchunks-adding-branches.gh (25.5 KB)

2 Likes

Actually the point method is an excellent and elegant way for people who don’t know coding.
I would definitely recommend doing like this.

1 Like

Yes, I’m truly sorry, I had indeed made a mistake in constructing my list manually. This goes to show why regular breaks from the screen are important. My well-meant apologies.

Above all I hope my explanation was clear. And it’s also interesting to see how it would be solved in python.

1 Like

Agree that coding is definitely preferable…much more elegant and direct.

@Adam_M 's point-based is clever, though. If you want something that’s purely list-based, this works as well:

makingchunks-adding-branches_v3.gh (29.9 KB)