You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
973 B

package day02
import (
"log"
"regexp"
"strconv"
"git.elis.nu/etu/aoc2020/utils"
)
type PasswordPolicy struct {
LowerIndex int
HigherIndex int
ValidationChar byte
Password string
}
var rows []PasswordPolicy
func ParseFile(input string) {
// Parse file
for _, line := range utils.GetLinesFromFile("day02/" + input + ".txt") {
re := regexp.MustCompile(`(\d+)-(\d+) (\w+): (\w+)`)
// Match line against regex
matches := re.FindStringSubmatch(line)
// Parse ints
index1, err1 := strconv.Atoi(string(matches[1]))
index2, err2 := strconv.Atoi(string(matches[2]))
if err1 != nil {
log.Println("Failed to parse entry", err1)
continue
}
if err2 != nil {
log.Println("Failed to parse entry", err2)
continue
}
// Store password policy in list
rows = append(rows, PasswordPolicy{
LowerIndex: index1,
HigherIndex: index2,
ValidationChar: matches[3][0],
Password: string(matches[4]),
})
}
}