2020 Day 2 Complete

This commit is contained in:
Brian Buller 2020-12-02 07:36:18 -06:00
parent cd94bf4a8f
commit 0642c298f3
4 changed files with 1104 additions and 0 deletions

1000
2020/day02/input Normal file

File diff suppressed because it is too large Load Diff

94
2020/day02/main.go Normal file
View File

@ -0,0 +1,94 @@
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
}

3
2020/day02/testinput Normal file
View File

@ -0,0 +1,3 @@
1-3 a: abcde
1-3 b: cdefg
2-9 c: ccccccccc

View File

@ -76,6 +76,13 @@ func GetArgNumber(i int) string {
return ""
}
func OptArgNumber(i int, def string) string {
if len(os.Args) > i {
return os.Args[i]
}
return def
}
func StdinToIntSlice() []int {
var ret []int
st := StdinToStringSlice()