Day 4 part 1
This commit is contained in:
parent
91ce1fc9f3
commit
82290bad36
3
main.go
3
main.go
@ -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
41
solutions/day04.go
Normal 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
|
||||
}
|
Loading…
Reference in New Issue
Block a user