-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day02.swift
53 lines (43 loc) · 1.4 KB
/
Day02.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
import Advent
import Foundation
import RegexBuilder
public enum Day02: Day {
public static let title = "Password Philosophy"
private static let regex = Regex {
TryCapture.integer
"-"
TryCapture.integer
" "
TryCapture.character
": "
Capture.string
}
public static func part1(_ input: Input) throws -> Int {
try input.lines.count(where: { input in
let match = try regex.match(in: input)
let lower = match.output.1
let upper = match.output.2
let character = match.output.3
let password = match.output.4
let count = password.count(of: character)
return (lower...upper).contains(count)
})
}
public static func part2(_ input: Input) throws -> Int {
try input.lines.count(where: { input in
let match = try regex.match(in: input)
let index1 = match.output.1 - 1
let index2 = match.output.2 - 1
let character = match.output.3
let password = match.output.4
let match1 = password.character(at: index1) == character
let match2 = password.character(at: index2) == character
return match1 != match2
})
}
}
extension String {
fileprivate func character(at offset: Int) -> Character {
self[index(startIndex, offsetBy: offset)]
}
}