From 2e617ff708fc40b416430bcd7c72accb4266c4c5 Mon Sep 17 00:00:00 2001 From: shillerben Date: Sat, 5 Jun 2021 00:05:13 -0500 Subject: [PATCH] Solved problem 26 --- .gitignore | 1 + 26/pe26.go | 11 ------- pe26/pe26.go | 82 +++++++++++++++++++++++++++++++++++++++++++++++++ projecteuler.go | 24 +++++++++++++++ 4 files changed, 107 insertions(+), 11 deletions(-) create mode 100644 .gitignore delete mode 100644 26/pe26.go create mode 100644 pe26/pe26.go create mode 100644 projecteuler.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cfa84bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +projecteuler diff --git a/26/pe26.go b/26/pe26.go deleted file mode 100644 index 5e0f8c4..0000000 --- a/26/pe26.go +++ /dev/null @@ -1,11 +0,0 @@ -package pe26 - -import ( - "fmt" -) - -func solve() { - for i := 1; i <= N; i++ { - - } -} diff --git a/pe26/pe26.go b/pe26/pe26.go new file mode 100644 index 0000000..a1f97e4 --- /dev/null +++ b/pe26/pe26.go @@ -0,0 +1,82 @@ +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) +} diff --git a/projecteuler.go b/projecteuler.go new file mode 100644 index 0000000..2f80426 --- /dev/null +++ b/projecteuler.go @@ -0,0 +1,24 @@ +package main + +import ( + "fmt" + "os" + + "shillerben.com/gitlab/shillerben/projecteuler/pe26" +) + +var solve_funcs = map[string]func([]string){ + "26": pe26.Solve, +} + +func main() { + if len(os.Args) < 3 { + fmt.Println("usage: projecteuler ") + return + } + + problem_number := os.Args[1] + problem_args := os.Args[2:] + + solve_funcs[problem_number](problem_args) +}