|
| 1 | +// |
| 2 | +// Scratchcards.swift |
| 3 | +// |
| 4 | +// Created by Matthew Judy on 2023-12-17. |
| 5 | +// |
| 6 | + |
| 7 | + |
| 8 | +import ArgumentParser |
| 9 | +import Foundation |
| 10 | +import Shared |
| 11 | + |
| 12 | + |
| 13 | +/// Day 4 : Scratchcards |
| 14 | +/// |
| 15 | +/// # Part One |
| 16 | +/// |
| 17 | +/// The gondola takes you up. Strangely, though, the ground doesn't seem to be |
| 18 | +/// coming with you; you're not climbing a mountain. As the circle of Snow |
| 19 | +/// Island recedes below you, an entire new landmass suddenly appears above |
| 20 | +/// you! The gondola carries you to the surface of the new island and lurches |
| 21 | +/// into the station. |
| 22 | +/// |
| 23 | +/// As you exit the gondola, the first thing you notice is that the air here is |
| 24 | +/// much **warmer** than it was on Snow Island. It's also quite **humid**. |
| 25 | +/// Is this where the water source is? |
| 26 | +/// |
| 27 | +/// The next thing you notice is an Elf sitting on the floor across the station |
| 28 | +/// in what seems to be a pile of colorful square cards. |
| 29 | +/// |
| 30 | +/// "Oh! Hello!" The Elf excitedly runs over to you. "How may I be of service?" |
| 31 | +/// You ask about water sources. |
| 32 | +/// |
| 33 | +/// "I'm not sure; I just operate the gondola lift. That does sound like |
| 34 | +/// something we'd have, though - this is **Island Island**, after all! I bet |
| 35 | +/// the **gardener** would know. He's on a different island, though - er, the |
| 36 | +/// small kind surrounded by water, not the floating kind. We really need to |
| 37 | +/// come up with a better naming scheme. Tell you what: if you can help me |
| 38 | +/// with something quick, I'll let you **borrow my boat** and you can go visit |
| 39 | +/// the gardener. I got all these scratchcards as a gift, but I can't figure |
| 40 | +/// out what I've won." |
| 41 | +/// |
| 42 | +/// The Elf leads you over to the pile of colorful cards. There, you discover |
| 43 | +/// dozens of scratchcards, all with their opaque covering already scratched |
| 44 | +/// off. Picking one up, it looks like each card has two lists of numbers |
| 45 | +/// separated by a vertical bar (|): a list of **winning numbers** and then a |
| 46 | +/// list of **numbers you have**. You organize the information into a table |
| 47 | +/// (your puzzle input). |
| 48 | +/// |
| 49 | +/// As far as the Elf has been able to figure out, you have to figure out which |
| 50 | +/// of the **numbers you have** appear in the list of **winning numbers*. The |
| 51 | +/// first match makes the card worth **one point** and each match after the |
| 52 | +/// first **doubles** the point value of that card. |
| 53 | +/// |
| 54 | +/// For example: |
| 55 | +/// |
| 56 | +/// ``` |
| 57 | +/// Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53 |
| 58 | +/// Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19 |
| 59 | +/// Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1 |
| 60 | +/// Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83 |
| 61 | +/// Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36 |
| 62 | +/// Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11 |
| 63 | +/// ``` |
| 64 | +/// |
| 65 | +/// In the above example, card 1 has: |
| 66 | +/// * five winning numbers (`41`, `48`, `83`, `86`,`17`) and |
| 67 | +/// * eight numbers you have (`83`, `86`, `6`, `31`, `17`, `9`, `48`, `53`). |
| 68 | +/// |
| 69 | +/// Of the numbers you have, four of them (`48`, `83`, `17`, `86`) are winning |
| 70 | +/// numbers! That means card 1 is worth **`8`** points (1 for the first match, |
| 71 | +/// then doubled three times for each of the three matches after the first). |
| 72 | +/// |
| 73 | +/// * Card 2 has two winning numbers (`32`, `61`), so it is worth |
| 74 | +/// **`2`** points. |
| 75 | +/// * Card 3 has two winning numbers (`1`, `21`), so it is worth |
| 76 | +/// **`2`** points. |
| 77 | +/// * Card 4 has one winning number (`84`), so it is worth |
| 78 | +/// **`1`** point. |
| 79 | +/// * Card 5 has no winning numbers, so it is worth |
| 80 | +/// no points. |
| 81 | +/// * Card 6 has no winning numbers, so it is worth |
| 82 | +/// no points. |
| 83 | +/// |
| 84 | +/// So, in this example, the Elf's pile of scratchcards is worth |
| 85 | +/// **`13`** points. |
| 86 | +/// |
| 87 | +/// Take a seat in the large pile of colorful cards. **How many points are |
| 88 | +/// they worth in total?** |
| 89 | +/// |
| 90 | +@main |
| 91 | +struct Scratchcards: AsyncParsableCommand |
| 92 | +{ |
| 93 | +// /// Adds a "sub-command" argument to the command, which allows the logic |
| 94 | +// /// to branch and handle requirements of either "Part One" or "Part Two". |
| 95 | +// /// The argument must be a value from this enumeration. |
| 96 | +// enum Mode: String, ExpressibleByArgument, CaseIterable |
| 97 | +// { |
| 98 | +// case <#modeA#> |
| 99 | +// case <#modeB#> |
| 100 | +// } |
| 101 | +// @Argument var mode: Mode |
| 102 | + |
| 103 | +// /// Adds a flag to the command, named for the behavioral difference in |
| 104 | +// /// "Part Two." This allows the command's logic to branch and handle the |
| 105 | +// /// requirements of either "Part One" or "Part Two". |
| 106 | +// @Flag(help: "Search for both cardinal values ('one', 'two', ...) and integers.") |
| 107 | +// var <#partTwoDifference#>: Bool = false |
| 108 | +} |
| 109 | + |
| 110 | + |
| 111 | +// MARK: - Command Execution |
| 112 | + |
| 113 | +extension Scratchcards |
| 114 | +{ |
| 115 | + mutating func run() async throws |
| 116 | + { |
| 117 | + let input: AsyncLineSequence = FileHandle.standardInput.bytes.lines // URL.homeDirectory.appending(path: "Desktop/input.txt").lines |
| 118 | + try await input.reduce(into: []) { $0.append($1) }.forEach { print($0) } |
| 119 | + } |
| 120 | +} |
| 121 | + |
0 commit comments