Last active
January 18, 2021 16:02
-
-
Save ohbarye/cb4b3ce3aba7e2d2b786919014f28668 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"os" | |
"strconv" | |
"strings" | |
) | |
func main() { | |
sf, _ := os.Open("./input.txt") | |
scanner := bufio.NewScanner(sf) | |
count := 0 | |
for scanner.Scan() { | |
if isValid(scanner.Text()) { | |
count++ | |
} | |
} | |
println(count) | |
} | |
func isValid(entry string) bool { | |
policy, password := split(entry, ": ") | |
times, char := split(policy, " ") | |
least, most := split(times, "-") | |
count := strings.Count(password, char) | |
leastCount, _ := strconv.Atoi(least) | |
mostCount, _ := strconv.Atoi(most) | |
return leastCount <= count && count <= mostCount | |
} | |
func split(str, sep string) (string, string) { | |
split := strings.Split(str, sep) | |
return split[0], split[1] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"os" | |
"strconv" | |
"strings" | |
) | |
func main() { | |
sf, _ := os.Open("./input.txt") | |
scanner := bufio.NewScanner(sf) | |
count := 0 | |
for scanner.Scan() { | |
if isValid(scanner.Text()) { | |
count++ | |
} | |
} | |
println(count) | |
} | |
func isValid(entry string) bool { | |
policy, password := split(entry, ": ") | |
times, char := split(policy, " ") | |
first, second := split(times, "-") | |
p1, _ := strconv.Atoi(first) | |
p2, _ := strconv.Atoi(second) | |
p1c := password[p1-1:p1] == char | |
p2c := password[p2-1:p2] == char | |
return (p1c || p2c) && !(p1c && p2c) | |
} | |
func split(str, sep string) (string, string) { | |
split := strings.Split(str, sep) | |
return split[0], split[1] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment