22 lines
669 B
Swift
22 lines
669 B
Swift
import Foundation
|
|
|
|
func readInput(filePath: String) -> ([Int], [Int]) {
|
|
let content = try? String(contentsOfFile: filePath, encoding: .ascii)
|
|
let lines = content!.split(separator: "\n")
|
|
var leftList: [Int] = []
|
|
var rightList: [Int] = []
|
|
for line in lines {
|
|
let lr = line.split(separator: " ")
|
|
leftList.append(Int(lr[0])!)
|
|
rightList.append(Int(lr[1])!)
|
|
}
|
|
return (leftList, rightList)
|
|
}
|
|
|
|
let (leftList, rightList) = readInput(filePath: CommandLine.arguments[1])
|
|
var counts: [Int: Int] = [:]
|
|
for n in rightList {
|
|
counts[n, default: 0] += 1
|
|
}
|
|
print(leftList.reduce(0, { sum, n in sum + n * counts[n, default: 0] }))
|