package main import ( "bytes" "fmt" "io/ioutil" "regexp" "strconv" ) func main() { total := 0 // Number of valid passwords r, _ := regexp.Compile(`(\d+)-(\d+) (\w): (.+)`) d, _ := ioutil.ReadFile("in2.txt") lines := bytes.Split(d, []byte("\n")) for _, l := range lines { parts := r.FindStringSubmatch(string(l)) if len(parts) != 0 { min, _ := strconv.Atoi(parts[1]) max, _ := strconv.Atoi(parts[2]) char := parts[3] password := parts[4] count := 0 for _, c := range password { if string(c) == char { count += 1 } } if count >= min && count <= max { total += 1 } } } fmt.Println(total) }