Day 7: Bridge Repair

Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL

FAQ

  • JRaccoon@discuss.tchncs.de
    link
    fedilink
    English
    arrow-up
    2
    ·
    5 days ago

    Java

    Today was pretty easy one but for some reason I spent like 20 minutes overthinking part 2 when all it needed was one new else if. I initially through the concatenation operator would take precedence even tho it clearly says “All operators are still evaluated left-to-right” in the instructions…

    I’m sure there are optimizations to do but using parallelStreams it only takes around 300ms total on my machine so there’s no point really

    The code
    import java.io.IOException;
    import java.nio.charset.StandardCharsets;
    import java.nio.file.Files;
    import java.nio.file.Path;
    import java.util.Arrays;
    import java.util.List;
    
    public class Day7 {
        public static void main(final String[] _args) throws IOException {
            final List<Equation> equations = Files.readAllLines(Path.of("2024\\07\\input.txt"), StandardCharsets.UTF_8).stream()
                .map(line -> line.split(":\\s"))
                .map(line -> new Equation(
                        Long.parseLong(line[0]),
                        Arrays.stream(line[1].split("\\s"))
                            .map(Integer::parseInt)
                            .toArray(Integer[]::new)
                    )
                ).toList();
    
            final char[] part1Operators = {'+', '*'};
            System.out.println("Part 1: " + equations.parallelStream()
                .mapToLong(equation -> getResultIfPossible(equation, part1Operators))
                .sum()
            );
    
            final char[] part2Operators = {'+', '*', '|'};
            System.out.println("Part 2: " + equations.parallelStream()
                .mapToLong(equation -> getResultIfPossible(equation, part2Operators))
                .sum()
            );
        }
    
        private static Long getResultIfPossible(final Equation equation, final char[] operators) {
            final var permutations = Math.pow(operators.length, equation.values.length - 1);
            for (int i = 0; i < permutations; i++) {
                long result = equation.values[0];
                int count = i;
    
                for (int j = 0; j < equation.values.length - 1; j++) {
                    // If the result is already larger than the expected one, we can short circuit here to save some time
                    if (result > equation.result) {
                        break;
                    }
    
                    final char operator = operators[count % operators.length];
                    count /= operators.length;
    
                    if (operator == '+') { result += equation.values[j + 1]; }
                    else if (operator == '*') { result *= equation.values[j + 1]; }
                    else if (operator == '|') { result = Long.parseLong(String.valueOf(result) + String.valueOf(equation.values[j + 1])); }
                    else {
                        throw new RuntimeException("Unsupported operator " + operator);
                    }
                }
    
                if (result == equation.result) {
                    return result;
                }
            }
    
            return 0L;
        }
    
        private static record Equation(long result, Integer[] values) {}
    }