-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.swift
64 lines (50 loc) · 1.74 KB
/
main.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import Foundation
import Algorithms
import Common
let input = Bundle.module.readLines(fromResource: "Input")
var gammaRate: [Character] = []
var epsilonRate: [Character] = []
for position in 0...11 {
let zeroes = input.filter { $0[position] == "0" }
let ones = input.filter { $0[position] == "1" }
if ones.count > zeroes.count {
gammaRate.append("1")
epsilonRate.append("0")
} else {
gammaRate.append("0")
epsilonRate.append("1")
}
}
let gammaRateValue = Int(String(gammaRate), radix: 2)!
let epsilonRateValue = Int(String(epsilonRate), radix: 2)!
print("Part one: \(gammaRateValue * epsilonRateValue)")
var oxygenGeneratorRating = input
var co2ScrubberRating = input
for position in 0...11 {
if oxygenGeneratorRating.count > 1 {
let zeroes = oxygenGeneratorRating.filter { $0[position] == "0" }
let ones = oxygenGeneratorRating.filter { $0[position] == "1" }
if ones.count >= zeroes.count {
oxygenGeneratorRating = ones
} else {
oxygenGeneratorRating = zeroes
}
}
if co2ScrubberRating.count > 1 {
let zeroes = co2ScrubberRating.filter { $0[position] == "0" }
let ones = co2ScrubberRating.filter { $0[position] == "1" }
if zeroes.count <= ones.count {
co2ScrubberRating = zeroes
} else {
co2ScrubberRating = ones
}
}
}
let oxygenGeneratorRatingValue = Int(oxygenGeneratorRating.first!, radix: 2)!
let co2ScrubberRatingValue = Int(co2ScrubberRating.first!, radix: 2)!
print("Part two: \(oxygenGeneratorRatingValue * co2ScrubberRatingValue)")
extension StringProtocol {
subscript(offset: Int) -> Character {
self[index(startIndex, offsetBy: offset)]
}
}