adventofcode/day2/two-2.go

39 lines
735 B
Go

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]
s := false
e := false
// Not utf8-safe! Bad practice, but it doesn't matter here
if string(password[min-1]) == char {
s = true
}
if string(password[max-1]) == char {
e = true
}
if s != e {
total += 1
}
}
}
fmt.Println(total)
}