day_7
The central idea of this is to represent each possible combination of n operators with an n-ary number.
So for part 1, which only uses two operators, we can use a binary number:
1011
would represent the combination of a + b * c + d + e
Part 2 uses three operators, so we can use a ternary number:
102
would represent the combination of a + b * c <concat> d
This way, we can range over all possible combinations of operators and apply them to the input.
Enumerating all possibilities isn’t the most efficient way, but the get_operator_combination
function is rather elegant.
Functions
pub fn get_operator_combinations(
input: List(Int),
num_operators: Int,
) -> List(List(fn(Int, Int) -> Int))
Encode n possible operators in m possible places