|
| 1 | +<?php |
| 2 | + |
| 3 | +require __DIR__ . '/../vendor/autoload.php'; |
| 4 | + |
| 5 | +use adventofcode\Year2024\MultiplyInstructionParser; |
| 6 | + |
| 7 | +/** |
| 8 | + * --- Day 3: Mull It Over --- |
| 9 | + * |
| 10 | + * --- Day 3: Mull It Over --- |
| 11 | + * |
| 12 | + * "Our computers are having issues, so I have no idea if we have any Chief Historians in stock! You're welcome to |
| 13 | + * check the warehouse, though," says the mildly flustered shopkeeper at the North Pole Toboggan Rental Shop. The |
| 14 | + * Historians head out to take a look. |
| 15 | + * |
| 16 | + * The shopkeeper turns to you. "Any chance you can see why our computers are having issues again?" |
| 17 | + * |
| 18 | + * The computer appears to be trying to run a program, but its memory (your puzzle input) is corrupted. All of the |
| 19 | + * instructions have been jumbled up! |
| 20 | + * |
| 21 | + * It seems like the goal of the program is just to multiply some numbers. It does that with instructions |
| 22 | + * like mul(X,Y), where X and Y are each 1-3 digit numbers. For instance, mul(44,46) multiplies 44 by 46 to get a |
| 23 | + * result of 2024. Similarly, mul(123,4) would multiply 123 by 4. |
| 24 | + * |
| 25 | + * However, because the program's memory has been corrupted, there are also many invalid characters that should be |
| 26 | + * ignored, even if they look like part of a mul instruction. Sequences like mul(4*, mul(6,9!, ?(12,34), |
| 27 | + * or mul ( 2 , 4 ) do nothing. |
| 28 | + * |
| 29 | + * For example, consider the following section of corrupted memory: |
| 30 | + * |
| 31 | + * xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5)) |
| 32 | + * |
| 33 | + * Only the four highlighted sections are real mul instructions. Adding up the result of each instruction |
| 34 | + * produces 161 (2*4 + 5*5 + 11*8 + 8*5). |
| 35 | + * |
| 36 | + * Scan the corrupted memory for uncorrupted mul instructions. What do you get if you add up all of the |
| 37 | + * results of the multiplications? |
| 38 | + */ |
| 39 | + |
| 40 | +$multiplyInstructionParser = new MultiplyInstructionParser(); |
| 41 | + |
| 42 | +$lines = file(__DIR__ . '/inputs/day-03.input', FILE_IGNORE_NEW_LINES); |
| 43 | + |
| 44 | +$sum = $multiplyInstructionParser->findMultiplyInstructionSum($lines); |
| 45 | +print('The sum of the multiplier instructions from the given input is ' . $sum . ".\n"); |
| 46 | + |
| 47 | +/** |
| 48 | + * --- Part Two --- |
| 49 | + * |
| 50 | + * As you scan through the corrupted memory, you notice that some of the conditional statements are also still intact. |
| 51 | + * If you handle some of the uncorrupted conditional statements in the program, you might be able to get an even more |
| 52 | + * accurate result. |
| 53 | + * |
| 54 | + * There are two new instructions you'll need to handle: |
| 55 | + * |
| 56 | + * - The do() instruction enables future mul instructions. |
| 57 | + * - The don't() instruction disables future mul instructions. |
| 58 | + * |
| 59 | + * Only the most recent do() or don't() instruction applies. At the beginning of the program, |
| 60 | + * mul instructions are enabled. |
| 61 | + * |
| 62 | + * For example: |
| 63 | + * |
| 64 | + * xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5)) |
| 65 | + * |
| 66 | + * This corrupted memory is similar to the example from before, but this time the mul(5,5) and mul(11,8) instructions |
| 67 | + * are disabled because there is a don't() instruction before them. The other mul instructions function normally, |
| 68 | + * including the one at the end that gets re-enabled by a do() instruction. |
| 69 | + * |
| 70 | + * This time, the sum of the results is 48 (2*4 + 8*5). |
| 71 | + * |
| 72 | + * Handle the new instructions; what do you get if you add up all of the results of just the enabled multiplications? |
| 73 | + */ |
| 74 | + |
| 75 | +$sum = $multiplyInstructionParser->findExtendedMultiplyInstructionSum($lines); |
| 76 | +print('The sum of the extended multiplier instructions from the given input is ' . $sum . ".\n"); |
0 commit comments