|
| 1 | +import ArgumentParser |
| 2 | +import Foundation |
| 3 | + |
| 4 | +extension String { |
| 5 | + var length: Int { |
| 6 | + return count |
| 7 | + } |
| 8 | + |
| 9 | + subscript (i: Int) -> String { |
| 10 | + return self[i ..< i + 1] |
| 11 | + } |
| 12 | + |
| 13 | + subscript (r: Range<Int>) -> String { |
| 14 | + let range = Range(uncheckedBounds: (lower: max(0, min(length, r.lowerBound)), |
| 15 | + upper: min(length, max(0, r.upperBound)))) |
| 16 | + let start = index(startIndex, offsetBy: range.lowerBound) |
| 17 | + let end = index(start, offsetBy: range.upperBound - range.lowerBound) |
| 18 | + return String(self[start ..< end]) |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +enum HandType: Int { |
| 23 | + case fiveOfKind = 6 |
| 24 | + case fourOfKind = 5 |
| 25 | + case fullHouse = 4 |
| 26 | + case threeOfKind = 3 |
| 27 | + case twoPairs = 2 |
| 28 | + case onePair = 1 |
| 29 | + case highCard = 0 |
| 30 | +} |
| 31 | + |
| 32 | +struct Hand { |
| 33 | + let cards: String |
| 34 | + let type: HandType |
| 35 | +} |
| 36 | + |
| 37 | +let values = ["2", "3", "4", "5", "6", "7", "8", "9", "T" ,"J", "Q", "K", "A"] |
| 38 | + |
| 39 | +func compareHands (lhs: String, rhs: String) -> Bool { |
| 40 | + for i in 0..<lhs.count { |
| 41 | + if lhs[i] == rhs[i] { |
| 42 | + continue |
| 43 | + } |
| 44 | + return values.firstIndex(of: lhs[i])! < values.firstIndex(of: rhs[i])! |
| 45 | + } |
| 46 | + return false |
| 47 | +} |
| 48 | + |
| 49 | +struct Bid { |
| 50 | + let hand: Hand |
| 51 | + let bid: Int |
| 52 | +} |
| 53 | + |
| 54 | +extension Hand: Comparable { |
| 55 | + static func < (lhs: Hand, rhs: Hand) -> Bool { |
| 56 | + if lhs.type.rawValue == rhs.type.rawValue { |
| 57 | + return compareHands(lhs: lhs.cards, rhs: rhs.cards) |
| 58 | + } |
| 59 | + return lhs.type.rawValue < rhs.type.rawValue |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +extension Bid: Comparable { |
| 64 | + static func < (lhs: Bid, rhs: Bid) -> Bool { |
| 65 | + return lhs.hand < rhs.hand |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +@main |
| 70 | +struct Day07: ParsableCommand { |
| 71 | + @Option(help: "Specify the input") |
| 72 | + public var input: String |
| 73 | + |
| 74 | + func type(_ hand: String) -> HandType { |
| 75 | + let cardsValues = Array(hand) |
| 76 | + let cardValueSet = Set(cardsValues) |
| 77 | + let cardValueCount = cardValueSet.map { value in |
| 78 | + cardsValues.filter { $0 == value }.count |
| 79 | + } |
| 80 | + |
| 81 | + let sortedCardValueCount = cardValueCount.sorted() |
| 82 | + let isFiveOfKind = sortedCardValueCount == [5] |
| 83 | + let isFourOfKind = sortedCardValueCount == [1, 4] |
| 84 | + let isFullHouse = sortedCardValueCount == [2, 3] |
| 85 | + let isThreeOfKind = sortedCardValueCount == [1, 1, 3] |
| 86 | + let isTwoPairs = sortedCardValueCount == [1, 2, 2] |
| 87 | + let isOnePair = sortedCardValueCount == [1, 1, 1, 2] |
| 88 | + let isHighCard = sortedCardValueCount == [1, 1, 1, 1, 1] |
| 89 | + if isFiveOfKind { |
| 90 | + return .fiveOfKind |
| 91 | + } else if isFourOfKind { |
| 92 | + return .fourOfKind |
| 93 | + } else if isFullHouse { |
| 94 | + return .fullHouse |
| 95 | + } else if isThreeOfKind { |
| 96 | + return .threeOfKind |
| 97 | + } else if isTwoPairs { |
| 98 | + return .twoPairs |
| 99 | + } else if isOnePair { |
| 100 | + return .onePair |
| 101 | + } else if isHighCard { |
| 102 | + return .highCard |
| 103 | + } |
| 104 | + return .highCard |
| 105 | + } |
| 106 | + |
| 107 | + public func run() throws { |
| 108 | + guard let filecontent = try? String(contentsOfFile: input, encoding: .utf8) |
| 109 | + else { |
| 110 | + print("Failed to open file \(input)") |
| 111 | + return |
| 112 | + } |
| 113 | + |
| 114 | + let lines = filecontent.components(separatedBy: .newlines) |
| 115 | + var bids: [Bid] = [] |
| 116 | + for line in lines { |
| 117 | + let cards = line.components(separatedBy: " ") |
| 118 | + let hand = Hand(cards: cards[0], type: type(cards[0])) |
| 119 | + bids.append(Bid(hand: hand, bid: Int(cards[1])!)) |
| 120 | + } |
| 121 | + bids.sort() |
| 122 | + var result = 0 |
| 123 | + for i in 0..<bids.count { |
| 124 | + print(bids[i].hand.cards) |
| 125 | + result += bids[i].bid * (i+1) |
| 126 | + } |
| 127 | + |
| 128 | + print(result) |
| 129 | + } |
| 130 | +} |
0 commit comments