adventofcode/2020/day02/main.go

95 lines
1.6 KiB
Go
Raw Permalink Normal View History

2020-12-02 13:36:18 +00:00
package main
import (
"fmt"
"strings"
h "git.bullercodeworks.com/brian/adventofcode/helpers"
)
func main() {
inp := h.StdinToStringSlice()
validate(inp, h.OptArgNumber(1, "1"))
}
func validate(input []string, p string) {
policy := h.Atoi(p)
var good, bad []string
for _, v := range input {
if policy == 1 {
if validSledRentalPolicy(v) {
good = append(good, v)
} else {
bad = append(bad, v)
}
} else {
if validTobogganPolicy(v) {
good = append(good, v)
} else {
bad = append(bad, v)
}
}
}
fmt.Printf("There are %d valid passwords\n", len(good))
}
func validSledRentalPolicy(line string) bool {
min := -1
max := -1
req := ""
var buff string
for i := 0; i < len(line); i++ {
if req != "" {
buff = buff + string(line[i])
continue
}
switch line[i] {
case '-':
min = h.Atoi(buff)
buff = ""
case ' ':
if max == -1 {
max = h.Atoi(buff)
buff = ""
}
case ':':
req = buff
buff = ""
default:
buff = buff + string(line[i])
}
}
reqCount := strings.Count(buff, req)
return reqCount >= min && reqCount <= max
}
func validTobogganPolicy(line string) bool {
idx1 := -1
idx2 := -1
req := byte(' ')
var buff string
for i := 0; i < len(line); i++ {
if req != ' ' && line[i] != ' ' {
buff = buff + string(line[i])
continue
}
switch line[i] {
case '-':
idx1 = h.Atoi(buff)
buff = ""
case ' ':
if idx2 == -1 {
idx2 = h.Atoi(buff)
buff = ""
}
case ':':
req = line[i-1]
buff = ""
default:
buff = buff + string(line[i])
}
}
match1, match2 := buff[idx1-1] == req, buff[idx2-1] == req
return match1 != match2
}