|
| 1 | +<?php |
| 2 | + |
| 3 | +require __DIR__ . '/../vendor/autoload.php'; |
| 4 | + |
| 5 | +use adventofcode\Year2024\ReactorReportParser; |
| 6 | + |
| 7 | +/** |
| 8 | + * --- Day 2: Red-Nosed Reports --- |
| 9 | + * |
| 10 | + * Fortunately, the first location The Historians want to search isn't a long walk from the Chief Historian's office. |
| 11 | + * |
| 12 | + * While the Red-Nosed Reindeer nuclear fusion/fission plant appears to contain no sign of the Chief Historian, the |
| 13 | + * engineers there run up to you as soon as they see you. Apparently, they still talk about the time Rudolph was saved |
| 14 | + * through molecular synthesis from a single electron. |
| 15 | + * |
| 16 | + * They're quick to add that - since you're already here - they'd really appreciate your help analyzing some unusual |
| 17 | + * data from the Red-Nosed reactor. You turn to check if The Historians are waiting for you, but they seem to have |
| 18 | + * already divided into groups that are currently searching every corner of the facility. You offer to help with the |
| 19 | + * unusual data. |
| 20 | + * |
| 21 | + * The unusual data (your puzzle input) consists of many reports, one report per line. Each report is a list of numbers |
| 22 | + * called levels that are separated by spaces. For example: |
| 23 | + * |
| 24 | + * 7 6 4 2 1 |
| 25 | + * 1 2 7 8 9 |
| 26 | + * 9 7 6 2 1 |
| 27 | + * 1 3 2 4 5 |
| 28 | + * 8 6 4 4 1 |
| 29 | + * 1 3 6 7 9 |
| 30 | + * |
| 31 | + * This example data contains six reports each containing five levels. |
| 32 | + * |
| 33 | + * The engineers are trying to figure out which reports are safe. The Red-Nosed reactor safety systems can only tolerate |
| 34 | + * levels that are either gradually increasing or gradually decreasing. So, a report only counts as safe if both of the |
| 35 | + * following are true: |
| 36 | + * |
| 37 | + * - The levels are either all increasing or all decreasing. |
| 38 | + * - Any two adjacent levels differ by at least one and at most three. |
| 39 | + * |
| 40 | + * In the example above, the reports can be found safe or unsafe by checking those rules: |
| 41 | + * |
| 42 | + * - 7 6 4 2 1: Safe because the levels are all decreasing by 1 or 2. |
| 43 | + * - 1 2 7 8 9: Unsafe because 2 7 is an increase of 5. |
| 44 | + * - 9 7 6 2 1: Unsafe because 6 2 is a decrease of 4. |
| 45 | + * - 1 3 2 4 5: Unsafe because 1 3 is increasing but 3 2 is decreasing. |
| 46 | + * - 8 6 4 4 1: Unsafe because 4 4 is neither an increase or a decrease. |
| 47 | + * - 1 3 6 7 9: Safe because the levels are all increasing by 1, 2, or 3. |
| 48 | + * |
| 49 | + * So, in this example, 2 reports are safe. |
| 50 | + * |
| 51 | + * Analyze the unusual data from the engineers. How many reports are safe? |
| 52 | + */ |
| 53 | + |
| 54 | +$reactorReportParser = new ReactorReportParser(); |
| 55 | + |
| 56 | +$reports = file(__DIR__ . '/inputs/day-02.input', FILE_IGNORE_NEW_LINES); |
| 57 | + |
| 58 | +$count = $reactorReportParser->findSafeReportsCount($reports); |
| 59 | +print('There are ' . $count . " safe reports.\n"); |
| 60 | + |
| 61 | +/** |
| 62 | + * --- Part Two --- |
| 63 | + * |
| 64 | + * The engineers are surprised by the low number of safe reports until they realize they forgot to tell you about |
| 65 | + * the Problem Dampener. |
| 66 | + * |
| 67 | + * The Problem Dampener is a reactor-mounted module that lets the reactor safety systems tolerate a single bad level |
| 68 | + * in what would otherwise be a safe report. It's like the bad level never happened! |
| 69 | + * |
| 70 | + * Now, the same rules apply as before, except if removing a single level from an unsafe report would make it safe, |
| 71 | + * the report instead counts as safe. |
| 72 | + * |
| 73 | + * More of the above example's reports are now safe: |
| 74 | + * |
| 75 | + * - 7 6 4 2 1: Safe without removing any level. |
| 76 | + * - 1 2 7 8 9: Unsafe regardless of which level is removed. |
| 77 | + * - 9 7 6 2 1: Unsafe regardless of which level is removed. |
| 78 | + * - 1 3 2 4 5: Safe by removing the second level, 3. |
| 79 | + * - 8 6 4 4 1: Safe by removing the third level, 4. |
| 80 | + * - 1 3 6 7 9: Safe without removing any level. |
| 81 | + * |
| 82 | + * Thanks to the Problem Dampener, 4 reports are actually safe! |
| 83 | + * |
| 84 | + * Update your analysis by handling situations where the Problem Dampener can remove a single level from unsafe reports. |
| 85 | + * How many reports are now safe? |
| 86 | + */ |
| 87 | + |
| 88 | +//$count = $reactorReportParser->findSafeReportsCount($reports, true); |
| 89 | +//print('There are ' . $count . " safe reports after applying the Problem Dampener.\n"); |
0 commit comments