Generating a list where no more than two identicle consecutive entries

As part of a different game, I’m trying to figure out a way to generate a list of 1s and 2s where there can’t be more than 2 consecutive of either 1 or 2.

e.g. [1,1,1,2,1,2,1,2] would not be ok because there are 3 1s next to eachother and [1,2,1,1,2,2,1,1] would be ok

https://flowlab.io/game/play/2507730

This is what I came up with so far but it seems to freeze but I can’t see the infinite loop if there is one.

1 Like

Well I’m not big on the maths, but the infinite loop is from the first output from the top filter, pass.

This should be pretty simple. How long do you want the list of numbers to be?

You have a lot going on in your code, so I’m not sure the implementation you’re going for, but you can easily strip repeating numbers by listing each and comparing them.

{"data":{"behavior":{"v":"2","nodes":[{"inputCount":0,"outputCount":1,"name":"Once","behaviorType":"logic.triggers.Once","x":180,"y":-576,"group":"","id":"4a7cbdc29bf90f45","resetOnLevelStart":false},{"inputCount":2,"outputCount":2,"name":"Repeater","behaviorType":"logic.logic.Repeater","x":360,"y":-592,"group":"","id":"4a7ccd428269f34e","notes":"List length","n_o":1,"repeatCount":10},{"inputCount":3,"outputCount":1,"name":"Random","behaviorType":"logic.logic.Random","x":540,"y":-656,"group":"","id":"4a7cedbca3fdbf4a","notes":"Random 1 or 2","n_o":1,"min":1,"max":2},{"inputCount":7,"outputCount":5,"name":"Number List","behaviorType":"logic.data.NumberList","x":720,"y":-592,"group":"","id":"4a7d299cab8d834a","notes":"List with no numbers removed","n_o":1,"startVal":[]},{"inputCount":1,"outputCount":2,"name":"Number List Each","behaviorType":"logic.data.ListEach","x":900,"y":-608,"group":"","id":"4a7ead682e428240","delay":0},{"gateVal":0,"mode":"equal to","inputCount":2,"outputCount":2,"name":"Filter","behaviorType":"logic.logic.Filter2","x":1080,"y":-624,"group":"","id":"4a7ec50b38a64945","notes":"Check if this number is the same as the last","n_o":1},{"inputCount":7,"outputCount":5,"name":"Number List","behaviorType":"logic.data.NumberList","x":1440,"y":-624,"group":"","id":"4a80712139b14143","notes":"Smaller list with repeats of 3 removed","n_o":1,"startVal":[]},{"inputCount":2,"outputCount":1,"name":"Limit Numbers","behaviorType":"logic.NodeGroup","x":1260,"y":-624,"group":"","id":"4a837bd417275d43","notes":"Counter","n_o":1,"isMenuItem":false},{"inputCount":3,"outputCount":1,"name":"Number","behaviorType":"logic.logic.Value","x":1260,"y":-656,"group":"4a837bd417275d43","id":"4a7f0f51a59f0648","startVal":1,"tag":"Add","roundMode":1},{"inputCount":3,"outputCount":1,"name":"Number","behaviorType":"logic.logic.Value","x":1440,"y":-688,"group":"4a837bd417275d43","id":"4a7f5c833dc9ef41","startVal":0,"tag":"Total #","roundMode":1},{"gateVal":2,"mode":"greater than or equal to","inputCount":2,"outputCount":2,"name":"Filter","behaviorType":"logic.logic.Filter2","x":1620,"y":-688,"group":"4a837bd417275d43","id":"4a7f93423498f74f","notes":"While the current number of repeating items is equal to or over 2, prevent new ones from being added to the list.","n_o":1},{"inputCount":3,"outputCount":1,"name":"Switch","behaviorType":"logic.logic.Switch","x":1800,"y":-512,"group":"4a837bd417275d43","id":"4a7ff1d885d58245","notes":"Limiter","n_o":1,"_startVal":0,"dataType":2},{"inputCount":3,"outputCount":1,"name":"Number","behaviorType":"logic.logic.Value","x":1260,"y":-752,"group":"4a837bd417275d43","id":"4a80448448644640","startVal":0,"tag":"Reset","roundMode":1},{"inputCount":0,"outputCount":1,"name":"Bundle Input","behaviorType":"logic.NodeGroupInput","x":1080,"y":-480,"group":"4a837bd417275d43","id":"4a8399fa130e6449","portId":"4a837bd417275d43i0","tag":"Same","dataType":2},{"inputCount":0,"outputCount":1,"name":"Bundle Input","behaviorType":"logic.NodeGroupInput","x":1080,"y":-560,"group":"4a837bd417275d43","id":"4a83c40c48a18841","portId":"4a837bd417275d43i1","tag":"Diff","dataType":2},{"inputCount":1,"outputCount":0,"name":"Bundle Output","behaviorType":"logic.NodeGroupOutput","x":1980,"y":-512,"group":"4a837bd417275d43","id":"4a8420c72c594345","portId":"4a837bd417275d43o0","tag":"Num","dataType":2}],"links":[{"input_id":"4a7ccd428269f34ei1","output_id":"4a7cbdc29bf90f45o0"},{"input_id":"4a7ccd428269f34ei1","output_id":"4a7cbdc29bf90f45o0"},{"input_id":"4a7cedbca3fdbf4ai0","output_id":"4a7ccd428269f34eo0"},{"input_id":"4a7d299cab8d834ai2","output_id":"4a7ccd428269f34eo1"},{"input_id":"4a7cedbca3fdbf4ai0","output_id":"4a7ccd428269f34eo0"},{"input_id":"4a7d299cab8d834ai1","output_id":"4a7cedbca3fdbf4ao0"},{"input_id":"4a7d299cab8d834ai1","output_id":"4a7cedbca3fdbf4ao0"},{"input_id":"4a7d299cab8d834ai2","output_id":"4a7ccd428269f34eo1"},{"input_id":"4a7ead682e428240i0","output_id":"4a7d299cab8d834ao0"},{"input_id":"4a7ead682e428240i0","output_id":"4a7d299cab8d834ao0"},{"input_id":"4a7ec50b38a64945i1","output_id":"4a7ead682e428240o0"},{"input_id":"4a7ec50b38a64945i0","output_id":"4a7ec50b38a64945o1"},{"input_id":"4a7ec50b38a64945i1","output_id":"4a7ead682e428240o0"},{"input_id":"4a837bd417275d43i0","output_id":"4a7ec50b38a64945o0"},{"input_id":"4a7ec50b38a64945i0","output_id":"4a7ec50b38a64945o1"},{"input_id":"4a837bd417275d43i1","output_id":"4a7ec50b38a64945o1"},{"input_id":"4a80712139b14143i1","output_id":"4a837bd417275d43o0"},{"input_id":"4a837bd417275d43i0","output_id":"4a7ec50b38a64945o0"},{"input_id":"4a837bd417275d43i1","output_id":"4a7ec50b38a64945o1"},{"input_id":"4a80712139b14143i1","output_id":"4a837bd417275d43o0"},{"input_id":"4a7f5c833dc9ef41i2","output_id":"4a7f0f51a59f0648o0"},{"input_id":"4a7f93423498f74fi1","output_id":"4a7f5c833dc9ef41o0"},{"input_id":"4a7ff1d885d58245i0","output_id":"4a7f93423498f74fo0"},{"input_id":"4a7ff1d885d58245i1","output_id":"4a7f93423498f74fo1"},{"input_id":"4a8420c72c594345i0","output_id":"4a7ff1d885d58245o0"},{"input_id":"4a7f5c833dc9ef41i0","output_id":"4a80448448644640o0"},{"input_id":"4a7f5c833dc9ef41i1","output_id":"4a80448448644640o0"},{"input_id":"4a7f0f51a59f0648i1","output_id":"4a8399fa130e6449o0"},{"input_id":"4a7ff1d885d58245i2","output_id":"4a8399fa130e6449o0"},{"input_id":"4a80448448644640i1","output_id":"4a83c40c48a18841o0"},{"input_id":"4a7ff1d885d58245i2","output_id":"4a83c40c48a18841o0"}]}}}

This code for example is a pretty easy way to get a new smaller list without repeating numbers of 3

2 Likes

Alternatively, if you are not feeding a list, but creating a brand new one, you can keep creating new numbers until it is not one of the repeating ones. This means you will always know the size of the list.

I want a list of 8 numbers where there are no more than 2 of the same number in a row (only two options needed, either 1 or 2). It seems like most ways I’ve tried end up finding an infinite loop somewhere even when I limit the count.

ones_count = 0;
twos_count = 0;
next_number = 0;
output = [];

for(let i = 0; i < 8; i++){
	next_number = Math.random(1,2);

	if(twos_count == 2){
		next_number = 1;
	if(ones_count == 2){
		next_number = 2;

	if (next_number == 1) {
		twos_count = 0;
		ones_count++;
	} else if (next_number == 2) {
		ones_count = 0
		twos_count++;
	}
	
	output.push(next_number);
}

wrote some pseudocode to represent the behaviour I’m looking for

1 Like

It does need to be a set length (8 for my use case but that’s arbitrary) so I can’t just strip a longer list unfortunately.

I had considered this, but didn’t like the idea that technically there’s a chance it goes on infitely and never generates the full list. when this should be an exercise in regex generation. I think it might be a timing/stepping issue with which blocks get updated before others.

2 Likes

Sure, I will make a bundle for this by the end of today.

No worries! I made a quick mock of my second idea as well. Here we just keep generating numbers until we reach the desired length. (This should follow a similar idea to your pseudo-code, but the method for accounting for multiple of the same number is slightly different)

{"data":{"behavior":{"v":"2","nodes":[{"inputCount":3,"outputCount":1,"name":"Random","behaviorType":"logic.logic.Random","x":450,"y":-192,"group":"","id":"4d9c4ae8ee666647","notes":"1 or 2","n_o":1,"min":1,"max":2},{"gateVal":0,"mode":"equal to","inputCount":2,"outputCount":2,"name":"Filter","behaviorType":"logic.logic.Filter2","x":630,"y":-192,"group":"","id":"4d9e68b2cdb80445","notes":"Check if this number is the same as the last","n_o":1},{"inputCount":7,"outputCount":5,"name":"Number List","behaviorType":"logic.data.NumberList","x":990,"y":-192,"group":"","id":"4d9e68b2c9eb8c4b","notes":"A list with repeats of 3 removed","n_o":1,"startVal":[]},{"inputCount":2,"outputCount":2,"name":"Limit Numbers","behaviorType":"logic.NodeGroup","x":810,"y":-192,"group":"","id":"4d9e68b29f5fa742","notes":"Counter","n_o":1,"isMenuItem":false},{"inputCount":3,"outputCount":1,"name":"Number","behaviorType":"logic.logic.Value","x":1260,"y":-688,"group":"4d9e68b29f5fa742","id":"4d9e68b30d9e7f45","startVal":1,"tag":"Add","roundMode":1},{"inputCount":3,"outputCount":1,"name":"Number","behaviorType":"logic.logic.Value","x":1440,"y":-768,"group":"4d9e68b29f5fa742","id":"4d9e68b3bb4f4249","startVal":0,"tag":"Total #","roundMode":1},{"gateVal":2,"mode":"greater than or equal to","inputCount":2,"outputCount":2,"name":"Filter","behaviorType":"logic.logic.Filter2","x":1620,"y":-768,"group":"4d9e68b29f5fa742","id":"4d9e68b3a5a42e45","notes":"While the current number of repeating items is equal to or over 2, prevent new ones from being added to the list.","n_o":1},{"inputCount":3,"outputCount":1,"name":"Switch","behaviorType":"logic.logic.Switch","x":1800,"y":-624,"group":"4d9e68b29f5fa742","id":"4d9e68b364e04846","notes":"Limiter","n_o":1,"_startVal":0,"dataType":2},{"inputCount":3,"outputCount":1,"name":"Number","behaviorType":"logic.logic.Value","x":1260,"y":-784,"group":"4d9e68b29f5fa742","id":"4d9e68b353c82d4c","startVal":0,"tag":"Reset","roundMode":1},{"inputCount":0,"outputCount":1,"name":"Bundle Input","behaviorType":"logic.NodeGroupInput","x":1080,"y":-432,"group":"4d9e68b29f5fa742","id":"4d9e68b36e920847","portId":"4d9e68b29f5fa742i0","tag":"Same","dataType":2},{"inputCount":0,"outputCount":1,"name":"Bundle Input","behaviorType":"logic.NodeGroupInput","x":1080,"y":-592,"group":"4d9e68b29f5fa742","id":"4d9e68b3649e244d","portId":"4d9e68b29f5fa742i1","tag":"Diff","dataType":2},{"inputCount":1,"outputCount":0,"name":"Bundle Output","behaviorType":"logic.NodeGroupOutput","x":1980,"y":-624,"group":"4d9e68b29f5fa742","id":"4d9e68b32225e141","portId":"4d9e68b29f5fa742o0","tag":"Num","dataType":2},{"inputCount":3,"outputCount":1,"name":"Switch","behaviorType":"logic.logic.Switch","x":1800,"y":-464,"group":"4d9e68b29f5fa742","id":"4dc03169d88c3448","notes":"We still need the list even if the number is not added.","n_o":1,"_startVal":0,"dataType":2},{"inputCount":1,"outputCount":0,"name":"Bundle Output","behaviorType":"logic.NodeGroupOutput","x":1980,"y":-448,"group":"4d9e68b29f5fa742","id":"4dc0c139d02bc74a","portId":"4d9e68b29f5fa742o1","tag":"Fail","dataType":2},{"inputCount":1,"outputCount":1,"name":"Number List Count","behaviorType":"logic.data.ListCount","x":1170,"y":-192,"group":"","id":"4dacbb0d0ba72245"},{"gateVal":8,"mode":"greater than or equal to","inputCount":2,"outputCount":2,"name":"Filter","behaviorType":"logic.logic.Filter2","x":1350,"y":-208,"group":"","id":"4dacd2a486986148","notes":"Check if the list is the desired length (8)","n_o":1},{"inputCount":1,"outputCount":0,"name":"Message","behaviorType":"logic.components.Message","x":1530,"y":-192,"group":"","id":"4dad4e4c39442048","notes":null,"n_o":0,"msg":"Again","route":"SendToSelf"},{"inputCount":0,"outputCount":1,"name":"Once","behaviorType":"logic.triggers.Once","x":270,"y":-208,"group":"","id":"4dc126f24b022d4d","resetOnLevelStart":false},{"inputCount":0,"outputCount":1,"name":"Mailbox","behaviorType":"logic.components.Mailbox","x":270,"y":-144,"group":"","id":"4dc1c5e4e4987b43","msg":"Again"}],"links":[{"input_id":"4d9c4ae8ee666647i0","output_id":"4dc126f24b022d4do0"},{"input_id":"4d9c4ae8ee666647i0","output_id":"4dc1c5e4e4987b43o0"},{"input_id":"4d9e68b2cdb80445i1","output_id":"4d9c4ae8ee666647o0"},{"input_id":"4d9e68b2cdb80445i0","output_id":"4d9e68b2cdb80445o1"},{"input_id":"4d9e68b2cdb80445i1","output_id":"4d9c4ae8ee666647o0"},{"input_id":"4d9e68b29f5fa742i0","output_id":"4d9e68b2cdb80445o0"},{"input_id":"4d9e68b2cdb80445i0","output_id":"4d9e68b2cdb80445o1"},{"input_id":"4d9e68b29f5fa742i1","output_id":"4d9e68b2cdb80445o1"},{"input_id":"4d9e68b2c9eb8c4bi1","output_id":"4d9e68b29f5fa742o0"},{"input_id":"4d9e68b2c9eb8c4bi2","output_id":"4d9e68b29f5fa742o0"},{"input_id":"4d9e68b2c9eb8c4bi2","output_id":"4d9e68b29f5fa742o1"},{"input_id":"4dacbb0d0ba72245i0","output_id":"4d9e68b2c9eb8c4bo0"},{"input_id":"4d9e68b29f5fa742i0","output_id":"4d9e68b2cdb80445o0"},{"input_id":"4d9e68b29f5fa742i1","output_id":"4d9e68b2cdb80445o1"},{"input_id":"4d9e68b2c9eb8c4bi1","output_id":"4d9e68b29f5fa742o0"},{"input_id":"4d9e68b2c9eb8c4bi2","output_id":"4d9e68b29f5fa742o0"},{"input_id":"4d9e68b2c9eb8c4bi2","output_id":"4d9e68b29f5fa742o1"},{"input_id":"4dacbb0d0ba72245i0","output_id":"4d9e68b2c9eb8c4bo0"},{"input_id":"4dacd2a486986148i1","output_id":"4dacbb0d0ba72245o0"},{"input_id":"4dacd2a486986148i1","output_id":"4dacbb0d0ba72245o0"},{"input_id":"4dad4e4c39442048i0","output_id":"4dacd2a486986148o1"},{"input_id":"4dad4e4c39442048i0","output_id":"4dacd2a486986148o1"},{"input_id":"4d9c4ae8ee666647i0","output_id":"4dc126f24b022d4do0"},{"input_id":"4d9c4ae8ee666647i0","output_id":"4dc1c5e4e4987b43o0"},{"input_id":"4d9e68b3bb4f4249i2","output_id":"4d9e68b30d9e7f45o0"},{"input_id":"4d9e68b3a5a42e45i1","output_id":"4d9e68b3bb4f4249o0"},{"input_id":"4d9e68b364e04846i0","output_id":"4d9e68b3a5a42e45o0"},{"input_id":"4dc03169d88c3448i1","output_id":"4d9e68b3a5a42e45o0"},{"input_id":"4d9e68b364e04846i1","output_id":"4d9e68b3a5a42e45o1"},{"input_id":"4dc03169d88c3448i0","output_id":"4d9e68b3a5a42e45o1"},{"input_id":"4d9e68b32225e141i0","output_id":"4d9e68b364e04846o0"},{"input_id":"4d9e68b3bb4f4249i0","output_id":"4d9e68b353c82d4co0"},{"input_id":"4d9e68b3bb4f4249i1","output_id":"4d9e68b353c82d4co0"},{"input_id":"4d9e68b30d9e7f45i1","output_id":"4d9e68b36e920847o0"},{"input_id":"4d9e68b364e04846i2","output_id":"4d9e68b36e920847o0"},{"input_id":"4dc03169d88c3448i2","output_id":"4d9e68b36e920847o0"},{"input_id":"4d9e68b353c82d4ci1","output_id":"4d9e68b3649e244do0"},{"input_id":"4d9e68b364e04846i2","output_id":"4d9e68b3649e244do0"},{"input_id":"4dc0c139d02bc74ai0","output_id":"4dc03169d88c3448o0"}]}}}

As a side note, behaviors do have a sequence of frame order, going from top-left to bottom-right. This isn’t explained anywhere, but is a good thing to keep in mind no matter how you’re coding. It’s even more important if you’re trying to implement one-frame code as it can have major impacts there.

lol looks like @Recryptech got there before me lol…

1 Like