Day 2 part 2
This commit is contained in:
parent
1d9b8196d9
commit
a2cb6ff664
@ -5,7 +5,7 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
func checkPassword(input string) bool {
|
||||
func passwordPolicy1(input string) bool {
|
||||
dashPosition := strings.Index(input, "-")
|
||||
spacePosition := strings.Index(input, " ")
|
||||
colonPosition := strings.Index(input, ":")
|
||||
@ -47,7 +47,7 @@ func Day02Part01(isTest bool) int {
|
||||
correctPasswords := 0
|
||||
|
||||
for _, i := range values {
|
||||
if checkPassword(i) {
|
||||
if passwordPolicy1(i) {
|
||||
correctPasswords += 1
|
||||
}
|
||||
}
|
||||
@ -55,6 +55,39 @@ func Day02Part01(isTest bool) int {
|
||||
return correctPasswords
|
||||
}
|
||||
|
||||
func Day02Part02(isTest bool) int {
|
||||
return -1
|
||||
func passwordPolicy2(input string) bool {
|
||||
dashPosition := strings.Index(input, "-")
|
||||
spacePosition := strings.Index(input, " ")
|
||||
colonPosition := strings.Index(input, ":")
|
||||
firstIdx, err := strconv.Atoi(input[:dashPosition])
|
||||
if err != nil {
|
||||
panic("Error converting to int")
|
||||
}
|
||||
firstIdx -= 1
|
||||
secondIdx, err := strconv.Atoi(input[dashPosition+1 : spacePosition])
|
||||
if err != nil {
|
||||
panic("Error converting to int")
|
||||
}
|
||||
secondIdx -= 1
|
||||
letter := input[spacePosition+1 : colonPosition]
|
||||
rest := input[colonPosition+2:]
|
||||
|
||||
firstIdxIsTrue := string(rest[firstIdx]) == letter
|
||||
secondIdxIsTrue := string(rest[secondIdx]) == letter
|
||||
|
||||
return (firstIdxIsTrue && !secondIdxIsTrue) || (!firstIdxIsTrue && secondIdxIsTrue)
|
||||
}
|
||||
|
||||
func Day02Part02(isTest bool) int {
|
||||
input := ReadInput("02", isTest)
|
||||
values := strings.Split(input, "\n")
|
||||
correctPasswords := 0
|
||||
|
||||
for _, i := range values {
|
||||
if passwordPolicy2(i) {
|
||||
correctPasswords += 1
|
||||
}
|
||||
}
|
||||
|
||||
return correctPasswords
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user