Снова мартышки — те, да не те. Эти позадорней, поживей, поактивней 🙂 Но решение чем-то схожее — соседи, операции, кидают банан чиселку, занимаются эквилибристикой. Для этой загадки обвязка получилась потолще, конечно.
Мартышкины ужимки
enum Action {
    ADD('+', Long::sum),
    SUBTRACT('-', (op1, op2) -> op1 - op2),
    DIVIDE('/', (op1, op2) -> op1 / op2),
    MULTIPLY('*', (op1, op2) -> op1 * op2);
    final char code;
    final BiFunction<Long, Long, Long> func;
    static final Map<Character, Action> OPS = Arrays.stream(Action.values())
        .collect(Collectors.toUnmodifiableMap(it -> it.code, it -> it));
    Action(char code, BiFunction<Long, Long, Long> func) {
        this.code = code;
        this.func = func;
    }
    BiFunction<Long, Long, Long> invert(boolean isPrev) {
        switch (this) {
            case ADD: return (op1, op2) -> op2 - op1;
            case MULTIPLY: return (op1, op2) -> op2 / op1;
            case SUBTRACT: return isPrev ? ADD.func : this.func;
            case DIVIDE: return isPrev ? MULTIPLY.func : this.func;
            default: return null;
        }
    }
}
 Читать далее Advent of Code 2022: Day 21 →