-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.swift
22 lines (19 loc) · 869 Bytes
/
main.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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)")
}