I’m doing advent of code 24 and I’m on day 7. I’m trying to generate all permutations of either an add symbol “+” or multiply symbol “*”. I’m just not really sure how to achieve it.
# Length can be anything from 2 to 10 here
len = result[:values] |> length()
# I thought I'd create a list of +'s and *'s here and then combine them using a generator
pluses = for _ <- 0..len-1, do: "+"
multis = for _ <- 0..len-1, do: "*"
I’ve been reading posts for a while but I’m now at a bit of a loss, perhaps my approach is completely wrong!
I know that there are various approaches to solving this particular problem. Here are my 2 cents.
Having already tackled this challenge, my first instinct was similar to what you’re aiming for: generating all possible combinations of + and * for a given list of numbers.
I find it helpful to visualize this type of combinatorial problem as a tree, where each path represents a choice of operator (+ or *). The most effective way to address tree problems is through recursive coding.
If you find yourself stuck, feel free to check out my solution here.
Good luck! I truly enjoy learning from these challenges.
Yeah that make a bit more sense to use recursion and use booleans to do the fanning out. Your solution is really nice. I guess I could use the same method to generate a list of all the possible combinations, but then you don’t need to