This commit is contained in:
2024-12-13 12:12:16 -08:00
parent bc4a4a852d
commit 4e11f94a22
4 changed files with 950 additions and 0 deletions

42
day07/d07p1.swift Normal file
View File

@@ -0,0 +1,42 @@
import Foundation
struct Equation {
let left: Int
let right: [Int]
init(from s: String) {
let split = s.split(separator: " ")
left = Int(split[0].filter { $0 != ":" })!
right = split.dropFirst().map { Int($0)! }
}
func valid(with ops: [(Int, Int) -> Int]) -> Bool {
if ops.count != right.count - 1 {
return false
}
return left == ops.enumerated().reduce(right[0], { acc, op in
op.1(acc, right[op.0+1])
})
}
}
func readInput(_ filePath: String) throws -> [Equation] {
let content = try String(contentsOfFile: filePath, encoding: .ascii)
return content.split(separator: "\n").map { Equation(from: String($0)) }
}
func combo<T>(of symbols: [T], len: Int) -> [[T]] {
if len == 0 {
return [[]]
}
let cdrs = combo(of: symbols, len: len - 1)
return symbols.flatMap { s in cdrs.map { cdr in [s] + cdr } }
}
let op: [(Int, Int) -> Int] = [(+), (*)]
let eqs = try readInput(CommandLine.arguments[1])
let answer = eqs
.filter { eq in
combo(of: op, len: eq.right.count - 1).contains { eq.valid(with: $0) }
}.reduce(0, { s, eq in s + eq.left })
print(answer)