projecteuler-go/pe26/pe26.go
2021-06-05 15:48:09 -05:00

82 lines
1.2 KiB
Go

package pe26
import (
"fmt"
"strconv"
)
func max(arr []int) int {
result := 0
for v := range arr {
if v > result {
result = v
}
}
return result
}
func get_next_digit(numerator, denominator int) int {
if numerator < denominator {
numerator *= 10
}
remainder := numerator % denominator
return remainder
}
func calc_digit_repeat(i int, c chan int) {
digit_length := map[int]int{
0: 0, 1: 0, 2: 0, 3: 0, 4: 0,
5: 0, 6: 0, 7: 0, 8: 0, 9: 0,
}
rem := 1
for {
rem = get_next_digit(rem, i)
if rem == 0 {
// no repeats
c <- 0
break
}
if digit_length[rem] > 0 {
// found a repeat
c <- digit_length[rem]
break
} else {
// new digit
// increment other non-zero digits
for k, v := range digit_length {
if v > 0 {
digit_length[k]++
}
}
digit_length[rem]++
}
}
}
func start_calc_digit_repeat(i int) chan int {
c := make(chan int)
go calc_digit_repeat(i, c)
return c
}
func Solve(args []string) {
N, _ := strconv.Atoi(args[0])
chan_list := make([]chan int, N)
for i := 1; i <= N; i++ {
chan_list[i-1] = start_calc_digit_repeat(i)
}
max, max_idx := 0, 1
for i := 0; i < N; i++ {
val := <-chan_list[i]
if val > max {
max = val
max_idx = i + 1
}
}
fmt.Println(max_idx)
}