|
| 1 | +// |
| 2 | +// GearRatios.swift |
| 3 | +// |
| 4 | +// Created by Matthew Judy on 2023-12-14. |
| 5 | +// |
| 6 | + |
| 7 | + |
| 8 | +import ArgumentParser |
| 9 | +import Foundation |
| 10 | +import Shared |
| 11 | + |
| 12 | + |
| 13 | +/// Day 3 : Gear Ratios |
| 14 | +/// |
| 15 | +/// # Part One |
| 16 | +/// |
| 17 | +/// You and the Elf eventually reach a gondola lift station; he says the |
| 18 | +/// gondola lift will take you up to the **water source**, but this is as far |
| 19 | +/// as he can bring you. You go inside. |
| 20 | +/// |
| 21 | +/// It doesn't take long to find the gondolas, but there seems to be a problem: |
| 22 | +/// they're not moving. |
| 23 | +/// |
| 24 | +/// "Aaah!" |
| 25 | +/// |
| 26 | +/// You turn around to see a slightly-greasy Elf with a wrench and a look of |
| 27 | +/// surprise. "Sorry, I wasn't expecting anyone! The gondola lift isn't working |
| 28 | +/// right now; it'll still be a while before I can fix it." You offer to help. |
| 29 | +/// |
| 30 | +/// The engineer explains that an engine part seems to be missing from the |
| 31 | +/// engine, but nobody can figure out which one. If you can **add up all the |
| 32 | +/// part numbers** in the engine schematic, it should be easy to work out which |
| 33 | +/// part is missing. |
| 34 | +/// |
| 35 | +/// The engine schematic (your puzzle input) consists of a visual |
| 36 | +/// representation of the engine. There are lots of numbers and symbols you |
| 37 | +/// don't really understand, but apparently **any number adjacent to a |
| 38 | +/// symbol**, even diagonally, is a "part number" and should be included in |
| 39 | +/// your sum. Periods (`.`) do not count as a symbol. |
| 40 | +/// |
| 41 | +/// Here is an example engine schematic: |
| 42 | +/// |
| 43 | +///``` |
| 44 | +/// 467..114.. |
| 45 | +/// ...*...... |
| 46 | +/// ..35..633. |
| 47 | +/// ......#... |
| 48 | +/// 617*...... |
| 49 | +/// .....+.58. |
| 50 | +/// ..592..... |
| 51 | +/// ......755. |
| 52 | +/// ...$.*.... |
| 53 | +/// .664.598.. |
| 54 | +/// ``` |
| 55 | +/// |
| 56 | +/// In this schematic, two numbers are **not** part numbers because they are |
| 57 | +/// not adjacent to a symbol: `114` (top right) and `58` (middle right). All |
| 58 | +/// other numbers are adjacent to a symbol and so are part numbers; their sum |
| 59 | +/// is **`4361`**. |
| 60 | +/// |
| 61 | +/// Of course, the actual engine schematic is much larger. **What is the sum of |
| 62 | +/// all of the part numbers in the engine schematic?** |
| 63 | +/// |
| 64 | +@main |
| 65 | +struct GearRatios: AsyncParsableCommand |
| 66 | +{ |
| 67 | +// /// Adds a `--mode` option to the command, which allows the command's logic |
| 68 | +// /// to branch and handle requirements of either "Part One" or "Part Two". |
| 69 | +// /// The option must be followed by a value from this enumeration. |
| 70 | +// enum Mode: String, ExpressibleByArgument, CaseIterable |
| 71 | +// { |
| 72 | +// case <#modeA#> |
| 73 | +// case <#modeB#> |
| 74 | +// } |
| 75 | +// @Option(help: "'<#modeA#>' or '<#modeB#>'") |
| 76 | +// var mode: Mode |
| 77 | + |
| 78 | +// /// Adds a flag to the command, named for the behavioral difference in |
| 79 | +// /// "Part Two." This allows the command's logic to branch and handle the |
| 80 | +// /// requirements of either "Part One" or "Part Two". |
| 81 | +// @Flag(help: "Search for both cardinal values ('one', 'two', ...) and integers.") |
| 82 | +// var <#partTwoDifference#>: Bool = false |
| 83 | +} |
| 84 | + |
| 85 | + |
| 86 | +// MARK: - Command Execution |
| 87 | + |
| 88 | +extension GearRatios |
| 89 | +{ |
| 90 | + mutating func run() async throws |
| 91 | + { |
| 92 | + let input: AsyncLineSequence = FileHandle.standardInput.bytes.lines // URL.homeDirectory.appending(path: "Desktop/input.txt").lines |
| 93 | + try await input.reduce(into: []) { $0.append($1) }.forEach { print($0) } |
| 94 | + } |
| 95 | +} |
| 96 | + |
0 commit comments