AoC2020/day2/day2.go
2020-12-05 14:43:34 +01:00

54 lines
1.0 KiB
Go

package main
import (
"AoC2020/helper"
"fmt"
"os"
"strconv"
"strings"
)
func main() {
args := os.Args[1:]
input, err := helper.GetInput(args[0])
if err != nil {
fmt.Println(err)
}
count := 0
countPart2 := 0
for _, row := range input {
if checkPassword(disassembleRow(row)) {
count++
}
if checkPasswordPart2(disassembleRow(row)) {
countPart2++
}
}
fmt.Println(count)
fmt.Println(countPart2)
}
func disassembleRow(row string) (int, int, string, string) {
fields := strings.Split(row, " ")
scope := strings.Split(fields[0], "-")
min, _ := strconv.Atoi(scope[0])
max, _ := strconv.Atoi(scope[1])
return min, max, string(fields[1][0]), fields[2]
}
func checkPassword(min int, max int, char string, password string) bool {
occurence := strings.Count(password, char)
return (min <= occurence && occurence <= max)
}
func checkPasswordPart2(p1 int, p2 int, char string, password string) bool {
count := 0
if string(password[p1-1]) == char {
count++
}
if string(password[p2-1]) == char {
count++
}
return count == 1
}