Day 4 part 1

master
Araozu 2024-02-27 09:06:01 -05:00
parent 91ce1fc9f3
commit 82290bad36
2 changed files with 44 additions and 0 deletions

View File

@ -15,6 +15,9 @@ func main() {
runAndBenchmark("03", "1", false, solutions.Day03Part01)
runAndBenchmark("03", "2", false, solutions.Day03Part02)
runAndBenchmark("04", "1", false, solutions.Day04Part01)
runAndBenchmark("04", "2", false, solutions.Day04Part02)
}
type execute func(bool) int

41
solutions/day04.go Normal file
View File

@ -0,0 +1,41 @@
package solutions
import (
"regexp"
"strings"
)
func Day04Part01(isTest bool) int {
input := ReadInput("04", isTest)
passports := strings.Split(input, "\n\n")
regex, _ := regexp.Compile("(\\w+:[\\w#]+)")
correctAmount := 0
for _, passport := range passports {
matches := regex.FindAllString(passport, -1)
values := make(map[string]string)
for _, match := range matches {
colonPosition := strings.Index(match, ":")
key := match[:colonPosition]
value := match[colonPosition+1:]
values[key] = value
}
_, cidMissing := values["cid"]
keysAmount := len(values)
if keysAmount == 8 || (keysAmount == 7 && !cidMissing) {
correctAmount += 1
}
}
return correctAmount
}
func Day04Part02(isTest bool) int {
return -1
}