-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution_2018_02.swift
80 lines (65 loc) · 2.4 KB
/
Solution_2018_02.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import Foundation
struct Solution_2018_02: Solution {
var input: Input
func run() throws {
let ids = try input.get()
.components(separatedBy: .whitespacesAndNewlines)
// ------- Part 1 -------
func countLetters(_ s:String) -> NSCountedSet {
return s.reduce(into: NSCountedSet()) { acc, c in
acc.add(c)
}
}
func solve(_ s:String) -> (Bool, Bool) {
let counted = countLetters(s)
let twice = counted.contains(where: { counted.count(for: $0) == 2 })
let thrice = counted.contains(where: { counted.count(for: $0) == 3 })
return (twice, thrice)
}
let b2i: (Bool) -> Int = { $0 ? 1 : 0 }
let (twice, thrice) = ids.reduce(into: (0,0), { (res, id) in
let (twice, thrice) = solve(id)
res.0 += b2i(twice)
res.1 += b2i(thrice)
})
let part1 = twice * thrice
print(part1)
// ------- Part 2 -------
func distance(_ s1: String, _ s2: String) -> Int {
//strings are the same length
return s1.indices.reduce(into: 0, { (res, i) in
res += b2i(s1[i] != s2[i])
})
}
func commonPart(_ s1: String, _ s2: String) -> String {
var resultString = s1
if let index = s1.indices.first(where: { s1[$0] != s2[$0] }) {
resultString.remove(at: index)
}
return resultString
}
//let lazyIds = ids.lazy
//let pairs = lazyIds.flatMap({ id1 in lazyIds.map ({ id2 in (id1, id2) }) })
//guard let pairIndex = pairs.firstIndex(where: { distance($0, $1) == 1 }) else {
// fatalError("Should have found a pair...")
//}
//
//let pair = pairs[pairIndex]
//let part2 = commonPart(pair.0, pair.1)
func solve2(_ ids: [String]) -> String {
for i in ids.indices {
for j in (i + 1)..<ids.endIndex {
if distance(ids[i], ids[j]) == 1 {
return commonPart(ids[i], ids[j])
}
}
}
fatalError("Shouldn't happen")
}
let part2 = solve2(ids)
print(part2)
// ------- Test -------
assert(part1 == 8715, "WA")
assert(part2 == "fvstwblgqkhpuixdrnevmaycd", "WA")
}
}