75 lines
1.2 KiB
Go
75 lines
1.2 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
func readFile(p string) string {
|
|
if cont, err := os.ReadFile(p); err != nil {
|
|
panic(err)
|
|
} else {
|
|
return strings.TrimSpace(string(cont))
|
|
}
|
|
}
|
|
|
|
func parseInt(s string) int {
|
|
if i, err := strconv.Atoi(s); err != nil {
|
|
panic(err)
|
|
} else {
|
|
return i
|
|
}
|
|
}
|
|
|
|
func main() {
|
|
// data := readFile("test")
|
|
data := readFile("input")
|
|
|
|
idSum := 0
|
|
|
|
for rr := range strings.SplitSeq(data, ",") {
|
|
parts := strings.Split(rr, "-")
|
|
|
|
from := parseInt(parts[0])
|
|
to := parseInt(parts[1])
|
|
|
|
idMap := map[string]bool{}
|
|
|
|
for number := from; number <= to; number++ {
|
|
candidate := strconv.Itoa(number)
|
|
|
|
numLen := len(candidate)
|
|
|
|
for testParts := 2; testParts <= numLen; testParts++ {
|
|
if numLen%testParts == 0 {
|
|
sliceLen := numLen / testParts
|
|
|
|
ref := candidate[:sliceLen]
|
|
|
|
isCode := true
|
|
|
|
for testPartIndex := 1; testPartIndex < testParts; testPartIndex++ {
|
|
if candidate[sliceLen*testPartIndex:sliceLen*(testPartIndex+1)] != ref {
|
|
isCode = false
|
|
|
|
break
|
|
}
|
|
}
|
|
|
|
if isCode && !idMap[candidate] {
|
|
idMap[candidate] = true
|
|
idSum += number
|
|
|
|
fmt.Println(number)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
fmt.Println(idSum)
|
|
}
|