-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday2.go
61 lines (47 loc) · 1.57 KB
/
day2.go
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
package main
import (
"fmt"
"strings"
)
func fastPolicyChecker(lines <-chan string, passwords chan<- passwordLine) {
for rawLine := range lines {
passwords <- policyCheck(rawLine, day2Part1PolicyChecker)
}
close(passwords)
}
// Day2Solution1 finds the number of valid passwords in the list
func Day2Solution1(puzzleInputs []string, results chan string) {
validPasswords := 0
lines := make(chan string, len(puzzleInputs))
passwords := make(chan passwordLine)
for _, rawLine := range puzzleInputs {
lines <- rawLine
}
close(lines)
go fastPolicyChecker(lines, passwords)
for password := range passwords {
if password.valid {
validPasswords++
}
}
results <- fmt.Sprintf("part1: Number of valid passwords: %v", validPasswords)
}
func day2Part1PolicyChecker(password string, policy passwordPolicy) bool {
matchCounts := strings.Count(password, string(policy.letter))
return policy.lowCount <= matchCounts && matchCounts <= policy.highCount
}
// Day2Solution2 finds the number of valid passwords with the new set of rules
func Day2Solution2(puzzleInputs []string, results chan string) {
validPasswords := 0
for _, rawLine := range puzzleInputs {
if policyCheck(rawLine, day2Part2PolicyChecker).valid {
validPasswords++
}
}
results <- fmt.Sprintf("part2: Number of valid passwords: %v", validPasswords)
}
func day2Part2PolicyChecker(password string, policy passwordPolicy) bool {
firstLetter := rune(password[policy.lowCount-1])
secondLetter := rune(password[policy.highCount-1])
return (firstLetter == policy.letter) != (secondLetter == policy.letter)
}