Started AOC 2020 - solved the first 4.5 days.
This commit is contained in:
parent
bab5f879b0
commit
04f29cdb4d
9 changed files with 389 additions and 0 deletions
60
2020/day02.go
Normal file
60
2020/day02.go
Normal file
|
@ -0,0 +1,60 @@
|
|||
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)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue