Skip to content

Instantly share code, notes, and snippets.

@spellgen
Created December 13, 2020 23:48
Show Gist options
  • Save spellgen/a7c4bd540d5ef3aeb24968f5f1cc5e49 to your computer and use it in GitHub Desktop.
Save spellgen/a7c4bd540d5ef3aeb24968f5f1cc5e49 to your computer and use it in GitHub Desktop.
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
"time"
)
func main() {
t1 := time.Now()
scanner := bufio.NewScanner(os.Stdin)
line := 0
for scanner.Scan() {
txt := scanner.Text()
line++
if line == 1 {
continue
}
var start int
var step int
for pos, v := range strings.Split(txt, ",") {
if v == "x" {
continue
}
mul, err := strconv.Atoi(v)
if err != nil {
panic(err)
}
if pos == 0 {
start = pos
step = mul
} else {
start, step = findOne(pos, mul, start, step)
}
}
fmt.Printf("t=%d (%s)\n", start, time.Now().Sub(t1))
}
}
func findOne(pos, mul, start, step int) (int, int) {
k := 0
for (start+k*step)%mul != ((mul-pos)%mul+mul)%mul {
k++ // upping the ante
}
return start + k*step, step * mul
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment