|
| 1 | +package aminetti.adventofcode2024.day07; |
| 2 | + |
| 3 | +import com.google.common.collect.Iterables; |
| 4 | +import org.apache.commons.lang3.StringUtils; |
| 5 | +import org.slf4j.Logger; |
| 6 | +import org.slf4j.LoggerFactory; |
| 7 | + |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.Arrays; |
| 10 | +import java.util.List; |
| 11 | + |
| 12 | +import static aminetti.adventofcode2024.day07.Day07.Op.MULT; |
| 13 | +import static com.google.common.collect.Lists.newArrayList; |
| 14 | + |
| 15 | +public class Day07 { |
| 16 | + private static final Logger LOGGER = LoggerFactory.getLogger(Day07.class); |
| 17 | + private List<String> input; |
| 18 | + private final List<Long> results = new ArrayList<>(); |
| 19 | + private final List<List<Long>> components = new ArrayList<>(); |
| 20 | + |
| 21 | + public Day07() { |
| 22 | + } |
| 23 | + |
| 24 | + public void parseInput(List<String> input) { |
| 25 | + this.input = input; |
| 26 | + |
| 27 | + for (String s : input) { |
| 28 | + String[] split = s.split(":"); |
| 29 | + results.add(Long.parseLong(split[0])); |
| 30 | + String[] split1 = split[1].split("\\s+"); |
| 31 | + components.add(Arrays.stream(split1).filter(StringUtils::isNotBlank).map(Long::parseLong).toList()); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + public long solvePart1() { |
| 36 | + long sum = 0; |
| 37 | + for (int i = 0; i < results.size(); i++) { |
| 38 | + Long result = results.get(i); |
| 39 | + long count = countOfCorrectOperators(result, components.get(i), List.of(), newArrayList()); |
| 40 | + LOGGER.debug("count for {}: {}", result, count); |
| 41 | + if (count > 0) { |
| 42 | + sum += result; |
| 43 | + } |
| 44 | + } |
| 45 | + return sum; |
| 46 | + } |
| 47 | + |
| 48 | + public enum Op { |
| 49 | + SUM, MULT; |
| 50 | + |
| 51 | + public String print() { |
| 52 | + return this == SUM ? "+" : "*"; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + |
| 57 | + private long countOfCorrectOperators(long result, List<Long> longs, List<Op> operators, List<Long> components) { |
| 58 | + if (longs.size() == 1) { |
| 59 | + Long lastEl = Iterables.getOnlyElement(longs); |
| 60 | + if (result == lastEl) { |
| 61 | + |
| 62 | + String fullOperation = "(".repeat(operators.size()) + lastEl; |
| 63 | + |
| 64 | + ArrayList<Op> mutableOps = newArrayList(operators); |
| 65 | + ArrayList<Long> mutableComponents = newArrayList(components); |
| 66 | + while (!mutableOps.isEmpty()) { |
| 67 | + fullOperation += mutableOps.removeLast().print() + "" + mutableComponents.removeLast() + ")"; |
| 68 | + } |
| 69 | + |
| 70 | + LOGGER.debug("Operation: {}", fullOperation); |
| 71 | + LOGGER.info("Found correct operators"); |
| 72 | + return 1; |
| 73 | + } |
| 74 | + |
| 75 | + return 0; |
| 76 | + |
| 77 | + } |
| 78 | + |
| 79 | + if (result <= 0 || longs.isEmpty()) { |
| 80 | + return 0; |
| 81 | + } |
| 82 | + |
| 83 | + |
| 84 | + ArrayList<Op> opsForSum = newArrayList(operators); |
| 85 | + opsForSum.add(Op.SUM); |
| 86 | + |
| 87 | + ArrayList<Op> opsForMult = newArrayList(operators); |
| 88 | + opsForMult.add(MULT); |
| 89 | + |
| 90 | + ArrayList<Long> next = newArrayList(longs); |
| 91 | + Long component = next.removeLast(); |
| 92 | + |
| 93 | + ArrayList<Long> nextComponents = newArrayList(components); |
| 94 | + nextComponents.add(component); |
| 95 | + |
| 96 | + long sumMult = 0; |
| 97 | + if (result % component == 0) { |
| 98 | + sumMult += countOfCorrectOperators(result / component, next, opsForMult, nextComponents); |
| 99 | + } |
| 100 | + return sumMult |
| 101 | + + countOfCorrectOperators(result - component, next, opsForSum, nextComponents); |
| 102 | + } |
| 103 | + |
| 104 | + public long solvePart2() { |
| 105 | + |
| 106 | + return 0; |
| 107 | + } |
| 108 | +} |
0 commit comments