Files
aoc24/day01/d01p2.swift
2024-12-11 23:50:18 -08:00

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