day 02 - first thing completely from nvim
This commit is contained in:
74
02/main.go
Normal file
74
02/main.go
Normal file
@@ -0,0 +1,74 @@
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user