-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.swift
48 lines (37 loc) · 1.27 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
import Foundation
func main() throws {
let isTestMode = CommandLine.arguments.contains("test")
let input: [String] = try readInput(fromTestFile: isTestMode, separator: "\n\n")
let groups = input.map({ Group($0) })
print("Part 1:", groups.map({ $0.totalNumberOfQuestionsAnsweredYesToByAnyone() }).sum())
print("Part 2:", groups.map({ $0.totalNumberOfQuestionsAnsweredYesToByEveryone() }).sum())
}
class Group {
typealias Person = [Character]
let people: [Person]
init(_ groupInput: String) {
people = groupInput.split(separator: "\n").map({ [Character]($0) })
}
func totalNumberOfQuestionsAnsweredYesToByAnyone() -> Int {
var result: Set<Character> = []
for p in people {
for c in p { result.insert(c) }
}
return result.count
}
func totalNumberOfQuestionsAnsweredYesToByEveryone() -> Int {
var result: Set<Character> = []
for p in people {
for c in p {
var includeChar = true
for p2 in people where !p2.contains(c) {
includeChar = false
break
}
if includeChar { result.insert(c) }
}
}
return result.count
}
}
Timer.time(main)