This commit is contained in:
2024-12-11 23:50:18 -08:00
commit 423632525d
4 changed files with 1042 additions and 0 deletions

21
day01/d01p2.swift Normal file
View File

@@ -0,0 +1,21 @@
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] }))