main.go (1221B)
1 package main 2 3 import ( 4 "bufio" 5 "fmt" 6 "os" 7 "regexp" 8 "strconv" 9 "strings" 10 ) 11 12 func readInput(filename string) (res []string) { 13 file, _ := os.Open(filename) 14 defer file.Close() 15 scanner := bufio.NewScanner(file) 16 for scanner.Scan() { 17 res = append(res, scanner.Text()) 18 } 19 return 20 } 21 22 func main() { 23 input := readInput("./input.txt") 24 // 603 25 fmt.Println(part1(input)) 26 // 404 27 fmt.Println(part2(input)) 28 } 29 30 func part1(input []string) (valid_count int) { 31 matcher := regexp.MustCompile(`(\d+)-(\d+) ([a-z]): ([a-z]+)`) 32 for _, line := range input { 33 matches := matcher.FindStringSubmatch(line) 34 count_low, _ := strconv.Atoi(matches[1]) 35 count_high, _ := strconv.Atoi(matches[2]) 36 count := strings.Count(matches[4], matches[3]) 37 if count >= count_low && count <= count_high { 38 valid_count += 1 39 } 40 } 41 return 42 } 43 44 func part2(input []string) (valid_count int) { 45 matcher := regexp.MustCompile(`(\d+)-(\d+) ([a-z]): ([a-z]+)`) 46 for _, line := range input { 47 matches := matcher.FindStringSubmatch(line) 48 idx_low, _ := strconv.Atoi(matches[1]) 49 idx_high, _ := strconv.Atoi(matches[2]) 50 if (matches[4][idx_low-1] == matches[3][0]) != 51 (matches[4][idx_high-1] == matches[3][0]) { 52 valid_count += 1 53 } 54 } 55 return 56 }