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

15
day01/d01p1.swift Normal file
View File

@@ -0,0 +1,15 @@
import Foundation
let filePath = CommandLine.arguments[1]
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])!)
}
leftList.sort()
rightList.sort()
print(zip(leftList, rightList).reduce(0, { sum, lr in sum + abs(lr.0 - lr.1) }))

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] }))

1000
day01/input.txt Normal file

File diff suppressed because it is too large Load Diff

6
day01/test.txt Normal file
View File

@@ -0,0 +1,6 @@
3 4
4 3
2 5
1 3
3 9
3 3