Need help with combinator logic: comparing arbitrary signals

Don't know how to use a machine? Looking for efficient setups? Stuck in a mission?
Post Reply
Pi-C
Smart Inserter
Smart Inserter
Posts: 1644
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Need help with combinator logic: comparing arbitrary signals

Post by Pi-C »

Hi, there! Please excuse me for using this not very clear topic, it's hard to come up with a proper one-liner describing my problem. :-)

I have two lines (red and green wires ), each holding arbitrary signals representing items (e.g. iron, stone, medium power poles) in arbitrary quantities (e.g. 5 iron, 1 stone, and 3 copper on green and 2 iron, 4 stone, and 2 medium power poles on red). Now I want to output the total amount of an item that is signalled via the red wire if any amount of this item is also signalled on the green wire -- so in my example the expected output would be 2 iron and 4 stone.

I can isolate the items that are signalled on both red and green:
  1. Feed green line into a decider combinator set to "Each > 0"/"Output Each: 1".
  2. Feed red line into another decider with the same settings.
  3. Direct the output of both combinators to the input of a third decider set to "Each = 2"/"Output Each:1".
Now the tricky part is to pass on the correct quantity. In my example, the output of the combinator chain would be "1 iron" and "1 stone" -- not the expected "2 iron" and "4 stone". My naive plan was to feed both the list computed by the three deciders and the red line into another combinator that outputs everything >1. However, that would also output "2 medium power poles" because "2 > 1" matches the condition. I somehow must find a way to cleanly filter out everything from the red line that has no match on the green one, and I have absolutely no clue how to go about it.

Does anybody have an idea how I could approach this?

User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3700
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: Need help with combinator logic: comparing arbitrary signals

Post by DaveMcW »

Convert every signal on the green wire to 1,000,000.

Add the 2 wires together.

Pass the result through a decider combinator: Each > 1,000,000 → Each (input count).

Pass the result through an arithmetic combinator: Each - 1,000,000 → Each.

Pi-C
Smart Inserter
Smart Inserter
Posts: 1644
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: Need help with combinator logic: comparing arbitrary signals

Post by Pi-C »

Thank you, that's an elegant solution! The first test on a small scale worked very well in my setup, now I'm preparing to go all the way.

Just for curiosity: Suppose I also wanted to output negative values -- is there a way to do this in one go? The obvious thing would be to use two combinator chains. The first branch would process "Each > 0", just like you suggested; the second would do the reverse for "Each < 0", converting green signals to -1,000,000 …, and finally discarding everything above 0. Could that be simplified somehow? Just for processing the positive signals I needed six combinators, meaning I'd need a dozen if I also wanted to get the negative signals. I'm probably using way too many combinators already, often in settings with only limited space, so saving on combinators helps against spaghetti-wiring induced headaches. :-)
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3700
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: Need help with combinator logic: comparing arbitrary signals

Post by DaveMcW »

Just use Each > 500,000 in the decider combinator to handle negative numbers.

Pi-C
Smart Inserter
Smart Inserter
Posts: 1644
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: Need help with combinator logic: comparing arbitrary signals

Post by Pi-C »

Perfect -- thanks a lot! :-)
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

Post Reply

Return to “Gameplay Help”