∞.ΔI„+-S.ιSÐм.ø.œJs>ù9Ýyãδ.ιJ.E_à
Extremely slow brute-force approach, so it's barely unable to complete any of the test cases resulting in 3..
Try it online.
Here a slightly faster but incorrect approach to at least show the three test cases work as intended: Try it online. The difference is the õ.ø
, which is Ðм.ø
in the full program. The õ.ø
surround the list of characters with a single leading/trailing empty string, whereas the Ðм.ø
surrounds it with multiple empty leading/trailing strings (up to the amount of digits in the total input, plus 2 for the +-
).
Explanation:
∞.Δ # Find the first positive integer which is truthy for:
I # Push the input-list [x,y,z]
„+-S # Push character-pair ["+","-"]
.ι # Interleave it with the input-list
# i.e. [1,10,30] → [1,"+",10,"-",30]
S # Convert it to a flattened list of characters
# → [1,"+",1,0,"-",3,0]
Ð # Triplicate this list
м # Remove all characters in this list from the list,
# leaving a list of empty strings of the same size
# i.e. [1,"+",1,0,"-",3,0] → ["","","","","","",""]
.ø # Surround the earlier list with these empty strings as
# leading/trailing items
# → ["","","","","","","",1,"+",1,0,"-",3,0,"","","","","","",""]
The examples in the rest of the explanation uses a single empty leading/trailing string, since it's otherwise a bit too verbose. The actual functionality remains the same.
.œ # Get all partitions of this list
# → [[[""],["1"],["+"],["1"],["0"],["-"],["3"],["0"],[""]],
# [[""],["1"],["+"],["1"],["0"],["-"],["3"],["0", ""]],
# ...,
# [["","1","+","1","0","-","3","0",""]]]
J # Join each inner-most list together to a string
# → [["","1","+","1","0","-","3","0",""],
# ["","1","+","1","0","-","3","0"],
# ...,
# ["1+10-30"]]
s # Swap so the current number is at the top of the stack
> # Increase it by 1
# i.e. 3 → 4
ù # Only keep the partitions of that size
# → [["","1","+","10-30"],
# ["","1","+1","0-30"],
# ...,
# ["1+10-","3","0",""]]
9Ý # Push list [0,1,2,3,4,5,6,7,8,9]
yã # Get the current number's cartesian product of this list
# i.e. 3 → [[0,0,0], [0,0,1], ..., [9,9,9]]
δ # Apply on the two lists double-vectorized:
.ι # Interleave the lists
# → [[["",0,"1",0,"+",0,"10-30"],["",0,"1",0,"+",1,"10-30"],...],
# [["",0,"1",0,"+1",0,"0-30"],["",0,"1",0,"+1",1,"0-30"],...],
# ...,
# [...,["1+10-",9,"3",9,"0",9,""]]]
J # Join every inner-most list to a string
# → [["010+010-30","010+110-30",...],
# ["010+100-30","010+110-30",...],
# ...,
# [...,"1+10-93909"]]
.E # Evaluate each as Python-code
# → [[-10,90,...],
# [80,90,...],
# ...,
# [...,-93898]]
_ # Check if any result is 0 (1 if 0; 0 otherwise)
à # And take the flattened maximum to check if any was truthy
# (after which the result is output implicitly)