|
| 1 | +<?php |
| 2 | + |
| 3 | +require __DIR__ . '/../vendor/autoload.php'; |
| 4 | + |
| 5 | +use adventofcode\Year2025\SafeCracker; |
| 6 | + |
| 7 | +/** |
| 8 | + * --- Day 1: Secret Entrance --- |
| 9 | + * |
| 10 | + * The Elves have good news and bad news. |
| 11 | + * |
| 12 | + * The good news is that they've discovered project management! This has given them the tools they need to prevent their |
| 13 | + * usual Christmas emergency. For example, they now know that the North Pole decorations need to be finished soon so |
| 14 | + * that other critical tasks can start on time. |
| 15 | + * |
| 16 | + * The bad news is that they've realized they have a different emergency: according to their resource planning, none of |
| 17 | + * them have any time left to decorate the North Pole! |
| 18 | + * |
| 19 | + * To save Christmas, the Elves need you to finish decorating the North Pole by December 12th. |
| 20 | + * |
| 21 | + * Collect stars by solving puzzles. Two puzzles will be made available on each day; the second puzzle is unlocked when |
| 22 | + * you complete the first. Each puzzle grants one star. Good luck! |
| 23 | + * |
| 24 | + * You arrive at the secret entrance to the North Pole base ready to start decorating. Unfortunately, the password seems |
| 25 | + * to have been changed, so you can't get in. A document taped to the wall helpfully explains: |
| 26 | + * |
| 27 | + * "Due to new security protocols, the password is locked in the safe below. Please see the attached document for the |
| 28 | + * new combination." |
| 29 | + * |
| 30 | + * The safe has a dial with only an arrow on it; around the dial are the numbers 0 through 99 in order. As you turn the |
| 31 | + * dial, it makes a small click noise as it reaches each number. |
| 32 | + * |
| 33 | + * The attached document (your puzzle input) contains a sequence of rotations, one per line, which tell you how to open |
| 34 | + * the safe. A rotation starts with an L or R which indicates whether the rotation should be to the left (toward lower |
| 35 | + * numbers) or to the right (toward higher numbers). Then, the rotation has a distance value which indicates how many |
| 36 | + * clicks the dial should be rotated in that direction. |
| 37 | + * |
| 38 | + * So, if the dial were pointing at 11, a rotation of R8 would cause the dial to point at 19. After that, a rotation of |
| 39 | + * L19 would cause it to point at 0. |
| 40 | + * |
| 41 | + * Because the dial is a circle, turning the dial left from 0 one click makes it point at 99. Similarly, turning the |
| 42 | + * dial right from 99 one click makes it point at 0. |
| 43 | + * |
| 44 | + * So, if the dial were pointing at 5, a rotation of L10 would cause it to point at 95. After that, a rotation of R5 |
| 45 | + * could cause it to point at 0. |
| 46 | + * |
| 47 | + * The dial starts by pointing at 50. |
| 48 | + * |
| 49 | + * You could follow the instructions, but your recent required official North Pole secret entrance security training |
| 50 | + * seminar taught you that the safe is actually a decoy. The actual password is the number of times the dial is left |
| 51 | + * pointing at 0 after any rotation in the sequence. |
| 52 | + * |
| 53 | + * For example, suppose the attached document contained the following rotations: |
| 54 | + * |
| 55 | + * L68 |
| 56 | + * L30 |
| 57 | + * R48 |
| 58 | + * L5 |
| 59 | + * R60 |
| 60 | + * L55 |
| 61 | + * L1 |
| 62 | + * L99 |
| 63 | + * R14 |
| 64 | + * L82 |
| 65 | + * |
| 66 | + * Following these rotations would cause the dial to move as follows: |
| 67 | + * |
| 68 | + * - The dial starts by pointing at 50. |
| 69 | + * - The dial is rotated L68 to point at 82. |
| 70 | + * - The dial is rotated L30 to point at 52. |
| 71 | + * - The dial is rotated R48 to point at 0. |
| 72 | + * - The dial is rotated L5 to point at 95. |
| 73 | + * - The dial is rotated R60 to point at 55. |
| 74 | + * - The dial is rotated L55 to point at 0. |
| 75 | + * - The dial is rotated L1 to point at 99. |
| 76 | + * - The dial is rotated L99 to point at 0. |
| 77 | + * - The dial is rotated R14 to point at 14. |
| 78 | + * - The dial is rotated L82 to point at 32. |
| 79 | + * |
| 80 | + * Because the dial points at 0 a total of three times during this process, the password in this example is 3. |
| 81 | + * |
| 82 | + * Analyze the rotations in your attached document. What's the actual password to open the door? |
| 83 | + */ |
| 84 | + |
| 85 | +$safeCracker = new SafeCracker(); |
| 86 | + |
| 87 | +$instructions = file(__DIR__ . '/inputs/day-01.input', FILE_IGNORE_NEW_LINES); |
| 88 | + |
| 89 | +$totalZeros = $safeCracker->findPointedAtZerosFromInstructions($instructions); |
| 90 | +print('The total times zero was pointed at was ' . $totalZeros . ".\n"); |
| 91 | + |
| 92 | +/** |
| 93 | + * --- Part Two --- |
| 94 | + * |
| 95 | + * You're sure that's the right password, but the door won't open. You knock, but nobody answers. You build a snowman |
| 96 | + * while you think. |
| 97 | + * |
| 98 | + * As you're rolling the snowballs for your snowman, you find another security document that must have fallen into |
| 99 | + * the snow: |
| 100 | + * |
| 101 | + * "Due to newer security protocols, please use password method 0x434C49434B until further notice." |
| 102 | + * |
| 103 | + * You remember from the training seminar that "method 0x434C49434B" means you're actually supposed to count the number |
| 104 | + * of times any click causes the dial to point at 0, regardless of whether it happens during a rotation or at the end |
| 105 | + * of one. |
| 106 | + * |
| 107 | + * Following the same rotations as in the above example, the dial points at zero a few extra times during its rotations: |
| 108 | + * |
| 109 | + * The dial starts by pointing at 50. |
| 110 | + * The dial is rotated L68 to point at 82; during this rotation, it points at 0 once. |
| 111 | + * The dial is rotated L30 to point at 52. |
| 112 | + * The dial is rotated R48 to point at 0. |
| 113 | + * The dial is rotated L5 to point at 95. |
| 114 | + * The dial is rotated R60 to point at 55; during this rotation, it points at 0 once. |
| 115 | + * The dial is rotated L55 to point at 0. |
| 116 | + * The dial is rotated L1 to point at 99. |
| 117 | + * The dial is rotated L99 to point at 0. |
| 118 | + * The dial is rotated R14 to point at 14. |
| 119 | + * The dial is rotated L82 to point at 32; during this rotation, it points at 0 once. |
| 120 | + * |
| 121 | + * In this example, the dial points at 0 three times at the end of a rotation, plus three more times during a rotation. |
| 122 | + * So, in this example, the new password would be 6. |
| 123 | + * |
| 124 | + * Be careful: if the dial were pointing at 50, a single rotation like R1000 would cause the dial to point at 0 ten |
| 125 | + * times before returning back to 50! |
| 126 | + * |
| 127 | + * Using password method 0x434C49434B, what is the password to open the door? |
| 128 | + */ |
| 129 | + |
| 130 | +$totalZeros = $safeCracker->findZerosHitFromInstructions($instructions); |
| 131 | +print('The total times zero was hit was ' . $totalZeros . ".\n"); |
0 commit comments