Skip to content

Instantly share code, notes, and snippets.

@ohbarye
Last active January 18, 2021 16:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ohbarye/cb4b3ce3aba7e2d2b786919014f28668 to your computer and use it in GitHub Desktop.
Save ohbarye/cb4b3ce3aba7e2d2b786919014f28668 to your computer and use it in GitHub Desktop.
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]
}
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