diff --git a/main.go b/main.go index e613d7c..d61d57e 100644 --- a/main.go +++ b/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 diff --git a/solutions/day04.go b/solutions/day04.go new file mode 100644 index 0000000..75b9a6e --- /dev/null +++ b/solutions/day04.go @@ -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 +}