-
Notifications
You must be signed in to change notification settings - Fork 0
/
02.go
57 lines (43 loc) · 1.13 KB
/
02.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// it was strange to write, but feels fine reading.
// feels like a barebones language
package main
import (
"bufio"
"fmt"
"log"
"os"
"regexp"
"strconv"
//"strings"
)
var regex = regexp.MustCompile(`(\d+)-(\d+) (.): (.*)`)
func main() {
numValidStrings := 0
file, err := os.Open("2.input")
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
if isValid(scanner.Text()) {
numValidStrings += 1
}
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
fmt.Println(numValidStrings)
}
func isValid(line string) bool {
matches := regex.FindStringSubmatch(line)
num1, _ := strconv.Atoi(matches[1])
num2, _ := strconv.Atoi(matches[2])
char := matches[3][0]
password := matches[4]
//count := strings.Count(password, char)
fmt.Printf("%d %d %s %#v %d\n", num1, num2, char, password)
first_char := password[num1 - 1]
second_char := password[num2 - 1]
return ((first_char == char) || (second_char == char)) && (first_char != second_char)
}