feat: unify invalid checks with exactTwo boolean parameter
This commit is contained in:
@@ -1,24 +1,34 @@
|
|||||||
package daytwo
|
package daytwo
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"advent-of-code/internal/registry"
|
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"advent-of-code/internal/registry"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
registry.Register("2025D2", ParseInput, PartOne, PartTwo)
|
registry.Register("2025D2", ParseInput, PartOne, PartTwo)
|
||||||
}
|
}
|
||||||
|
|
||||||
func isInvalidID(id int) bool {
|
func isInvalid(id int, exactTwo bool) bool {
|
||||||
sID := strconv.Itoa(id)
|
sID := strconv.Itoa(id)
|
||||||
if len(sID)%2 != 0 {
|
if exactTwo {
|
||||||
return false
|
|
||||||
}
|
|
||||||
half := len(sID) / 2
|
half := len(sID) / 2
|
||||||
return sID[:half] == sID[half:]
|
return sID[:half] == sID[half:]
|
||||||
}
|
}
|
||||||
|
for patternLength := 1; patternLength <= len(sID)/2; patternLength++ {
|
||||||
|
repetitions := len(sID) / patternLength
|
||||||
|
if repetitions < 2 || len(sID)%patternLength != 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if strings.Repeat(sID[:patternLength], repetitions) == sID {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
func ParseInput(filepath string) []string {
|
func ParseInput(filepath string) []string {
|
||||||
content, _ := os.ReadFile(filepath)
|
content, _ := os.ReadFile(filepath)
|
||||||
@@ -33,7 +43,7 @@ func PartOne(input []string) int {
|
|||||||
start, _ := strconv.Atoi(parts[0])
|
start, _ := strconv.Atoi(parts[0])
|
||||||
end, _ := strconv.Atoi(parts[1])
|
end, _ := strconv.Atoi(parts[1])
|
||||||
for id := start; id <= end; id++ {
|
for id := start; id <= end; id++ {
|
||||||
if isInvalidID(id) {
|
if isInvalid(id, true) {
|
||||||
sum += id
|
sum += id
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user