d01
This commit is contained in:
15
day01/d01p1.swift
Normal file
15
day01/d01p1.swift
Normal 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
21
day01/d01p2.swift
Normal 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
1000
day01/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
6
day01/test.txt
Normal file
6
day01/test.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
3 4
|
||||
4 3
|
||||
2 5
|
||||
1 3
|
||||
3 9
|
||||
3 3
|
Reference in New Issue
Block a user