Skip to content
Permalink
main
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
import Foundation
import Shared
if let input = try Bundle.module.readFile("data/input.dat") {
let boardingPassIds:[Int] = input.components(separatedBy: .newlines)
.compactMap {
let binary = $0.replacingOccurrences(of: "F", with: "0")
.replacingOccurrences(of: "B", with: "1")
.replacingOccurrences(of: "L", with: "0")
.replacingOccurrences(of: "R", with: "1")
return Int(binary, radix: 2)
}
.sorted()
// Part 1: find the highest id
print("Part 1: \(boardingPassIds.last!)")
// Part 2: find the first non-consecutive id
let idealSum = (boardingPassIds.count + 1) * (boardingPassIds.first! + boardingPassIds.last!) / 2
let actualSum = boardingPassIds.reduce(0, +)
print("Part 2: \(idealSum - actualSum)")
}