|
| 1 | +import Algorithms |
| 2 | + |
| 3 | +struct Calibration { |
| 4 | + var result: Int |
| 5 | + var values: [Int] |
| 6 | +} |
| 7 | +extension Calibration { |
| 8 | + init<S: StringProtocol>(from line: S) { |
| 9 | + let parts = line.split(separator: ":") |
| 10 | + result = Int(parts[0]) ?? 0 |
| 11 | + values = parts[1].integers() |
| 12 | + } |
| 13 | +} |
| 14 | +extension Calibration: CustomStringConvertible { |
| 15 | + var description: String { |
| 16 | + "\(self.result): \(self.values.map {String($0)}.joined(separator: " "))" |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +struct Day07: AdventDay { |
| 21 | + var calibrations: [Calibration] |
| 22 | + init(data: String) { |
| 23 | + calibrations = data.lines().map { Calibration(from: $0) } |
| 24 | + } |
| 25 | + |
| 26 | + func isValidCalibration(_ calibration: Calibration) -> Bool { |
| 27 | + // early return |
| 28 | + if calibration.values.count == 1 { |
| 29 | + return calibration.result == calibration.values[0] |
| 30 | + } |
| 31 | + // generate all operators permutations using a cartesian product method |
| 32 | + let allPermutations = product(["+", "*"], repeated: calibration.values.count - 1) |
| 33 | + for ops in allPermutations { |
| 34 | + var res = calibration.values[0] |
| 35 | + let products = zip(ops, calibration.values[1..<calibration.values.count]) |
| 36 | + for (op, value) in products { |
| 37 | + if op == "+" { |
| 38 | + res += value |
| 39 | + } |
| 40 | + if op == "*" { |
| 41 | + res *= value |
| 42 | + } |
| 43 | + } |
| 44 | + // print(calibration, "->", res) |
| 45 | + if res == calibration.result { |
| 46 | + |
| 47 | + // early from the loop |
| 48 | + return true |
| 49 | + } |
| 50 | + } |
| 51 | + // none of the permutations allowed to get the expected result |
| 52 | + return false |
| 53 | + } |
| 54 | + |
| 55 | + func part1() -> Int { |
| 56 | + let possibleCalibrations = calibrations.filter { |
| 57 | + isValidCalibration($0) |
| 58 | + } |
| 59 | + |
| 60 | + return possibleCalibrations.map { $0.result }.sum |
| 61 | + } |
| 62 | + |
| 63 | + func isValidCalibration2(_ calibration: Calibration) -> Bool { |
| 64 | + // early return |
| 65 | + if calibration.values.count == 1 { |
| 66 | + return calibration.result == calibration.values[0] |
| 67 | + } |
| 68 | + // generate all operators permutations using a cartesian product method |
| 69 | + let allPermutations = product(["+", "*", "||"], repeated: calibration.values.count - 1) |
| 70 | + for ops in allPermutations { |
| 71 | + var res = calibration.values[0] |
| 72 | + let products = zip(ops, calibration.values[1..<calibration.values.count]) |
| 73 | + for (op, value) in products { |
| 74 | + if op == "+" { |
| 75 | + res += value |
| 76 | + } |
| 77 | + if op == "*" { |
| 78 | + res *= value |
| 79 | + } |
| 80 | + if op == "||" { |
| 81 | + res = Int(String(res) + String(value)) ?? 0 |
| 82 | + } |
| 83 | + } |
| 84 | + // print(calibration, "->", res) |
| 85 | + if res == calibration.result { |
| 86 | + |
| 87 | + // early from the loop |
| 88 | + return true |
| 89 | + } |
| 90 | + } |
| 91 | + // none of the permutations allowed to get the expected result |
| 92 | + return false |
| 93 | + } |
| 94 | + |
| 95 | + func part2() -> Int { |
| 96 | + let possibleCalibrations = calibrations.filter { calibration in |
| 97 | + if isValidCalibration(calibration) { |
| 98 | + return true |
| 99 | + } else { |
| 100 | + return isValidCalibration2(calibration) |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + return possibleCalibrations.map { $0.result }.sum |
| 105 | + } |
| 106 | +} |
0 commit comments