Day 2: Part 2
This commit is contained in:
1
main.go
1
main.go
@@ -13,4 +13,5 @@ func main() {
|
||||
|
||||
// Day 2
|
||||
fmt.Println(two.PartOne())
|
||||
fmt.Println(two.PartTwo())
|
||||
}
|
||||
|
@@ -65,10 +65,20 @@ func validate(password string, policy passwordPolicy) bool {
|
||||
return (instances >= policy.min && instances <= policy.max)
|
||||
}
|
||||
|
||||
func (d *db) validate() int {
|
||||
func tobogganValidate(password string, policy passwordPolicy) bool {
|
||||
var first, second string
|
||||
first = string(password[policy.min-1])
|
||||
if len(password) >= policy.max {
|
||||
second = string(password[policy.max-1])
|
||||
}
|
||||
|
||||
return ((first == policy.mustHave && second != policy.mustHave) || (first != policy.mustHave && second == policy.mustHave))
|
||||
}
|
||||
|
||||
func (d *db) validate(validator func(string, passwordPolicy) bool) int {
|
||||
valid := 0
|
||||
for _, entry := range d.passwords {
|
||||
if validate(entry.password, entry.policy) {
|
||||
if validator(entry.password, entry.policy) {
|
||||
valid++
|
||||
}
|
||||
}
|
||||
@@ -80,11 +90,14 @@ func (d *db) validate() int {
|
||||
func PartOne() string {
|
||||
database := db{}
|
||||
database.load("two/passwords.txt")
|
||||
valid := database.validate()
|
||||
valid := database.validate(validate)
|
||||
return fmt.Sprintf("Found %d valid passwords", valid)
|
||||
}
|
||||
|
||||
// PartTwo
|
||||
// PartTwo Find how many passwords are valid with the new interpretation of the policy
|
||||
func PartTwo() string {
|
||||
return ""
|
||||
database := db{}
|
||||
database.load("two/passwords.txt")
|
||||
valid := database.validate(tobogganValidate)
|
||||
return fmt.Sprintf("Found %d valid passwords", valid)
|
||||
}
|
||||
|
@@ -19,10 +19,22 @@ func Test_db_valiate(t *testing.T) {
|
||||
database := db{}
|
||||
database.load("sample.txt")
|
||||
|
||||
validCount := database.validate()
|
||||
validCount := database.validate(validate)
|
||||
|
||||
if validCount != 2 {
|
||||
t.Logf("Expected 2 valid passwords, Got %d valid passwords", validCount)
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
||||
|
||||
func Test_db_tobogganValidate(t *testing.T) {
|
||||
database := db{}
|
||||
database.load("sample.txt")
|
||||
|
||||
validCount := database.validate(tobogganValidate)
|
||||
|
||||
if validCount != 1 {
|
||||
t.Logf("Expected 1 valid password, Got %d valid passwords", validCount)
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user