adventofcode/2020/day02.go

61 lines
1.0 KiB
Go

package main
import (
"fmt"
"log"
"os"
"strconv"
"strings"
"git.annabunch.es/annabunches/adventofcode/2020/lib/fileutils"
)
func checkLine(line string, step string) int {
if line == "" {
return 0
}
data := strings.Split(line, " ")
minMaxData := strings.Split(data[0], "-")
min, err := strconv.Atoi(minMaxData[0])
if err != nil {
log.Panicf(err.Error())
}
max, err := strconv.Atoi(minMaxData[1])
if err != nil {
log.Panicf(err.Error())
}
char := strings.TrimSuffix(data[1], ":")
switch step {
case "1":
count := strings.Count(data[2], char)
if count >= min && count <= max {
return 1
}
return 0
case "2":
bchar := char[0]
if (data[2][min-1] == bchar || data[2][max-1] == bchar) &&
!(data[2][min-1] == bchar && data[2][max-1] == bchar) {
return 1
}
return 0
}
return 0
}
func main() {
step := os.Args[1]
values := fileutils.InputParserStrings(os.Args[2])
total := 0
for _, line := range values {
total += checkLine(line, step)
}
fmt.Println("Valid passwords:", total)
}