diff options
| author | Michael Manring <michael@manring> | 2022-04-08 15:09:50 +0700 |
|---|---|---|
| committer | Michael Manring <michael@manring> | 2022-04-08 15:09:50 +0700 |
| commit | 62740a926c09c61e812ab59741186316d54e1c60 (patch) | |
| tree | 8a6bd8d40e11cd836ade5e5834b74c8b4ad727e8 | |
| parent | 92bee2e691cf2967a56fb8b58c85a07495cf6320 (diff) | |
| download | perlweeklychallenge-club-62740a926c09c61e812ab59741186316d54e1c60.tar.gz perlweeklychallenge-club-62740a926c09c61e812ab59741186316d54e1c60.tar.bz2 perlweeklychallenge-club-62740a926c09c61e812ab59741186316d54e1c60.zip | |
pwc151 solution in go
| -rw-r--r-- | challenge-151/pokgopun/go/ch-1.go | 63 | ||||
| -rw-r--r-- | challenge-151/pokgopun/go/ch-2.go | 76 |
2 files changed, 139 insertions, 0 deletions
diff --git a/challenge-151/pokgopun/go/ch-1.go b/challenge-151/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..8df146b769 --- /dev/null +++ b/challenge-151/pokgopun/go/ch-1.go @@ -0,0 +1,63 @@ +package main + +import ( + "fmt" + "log" + "math" + "os" + "regexp" + "sort" + "strconv" + "strings" +) + +func main() { + samples := os.Args[1:] + if len(samples) == 0 { + samples = []string{ + "1 | 2 3 | 4 5", + "1 | 2 3 | 4 * * 5 | * 6", + } + } + re := regexp.MustCompile(`\S+`) + for _, sample := range samples { + fmt.Printf("Input: '%v'\n", sample) + levels := strings.Split(sample, "|") + nodes := make([]string, int(math.Pow(2, float64(len(levels))))) + //fmt.Println(nodes) + for i := len(levels) - 1; i >= 0; i-- { + ns := make([]string, int(math.Pow(2, float64(i)))) + copy(ns, re.FindAllString(levels[i], -1)) + //fmt.Println(ns) + for j := 0; j < len(ns); j++ { + s := []int{} + for _, v := range nodes[2*j : 2*j+2] { + if v == "" || v == "*" { + continue + } + n, err := strconv.Atoi(v) + if err != nil { + log.Fatal(err) + } + s = append(s, n) + } + //fmt.Println(" s =", s) + if len(s) != 0 { + sort.SliceStable(s, func(i, j int) bool { + return s[i] < s[j] + }) + ns[j] = strconv.Itoa(s[0]) + } else { + if ns[j] == "*" || ns[j] == "" { + ns[j] = "*" + } else { + ns[j] = strconv.Itoa(i + 1) + } + } + } + //fmt.Println("=>", ns) + nodes = ns + } + fmt.Printf("Output: %v\n\n", nodes[0]) + } +} diff --git a/challenge-151/pokgopun/go/ch-2.go b/challenge-151/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..676865f0ad --- /dev/null +++ b/challenge-151/pokgopun/go/ch-2.go @@ -0,0 +1,76 @@ +package main + +import ( + "fmt" + "log" + "os" + "strconv" +) + +func main() { + const debug = false + sos := [][]int{argInts()} + if len(sos[0]) == 0 { + sos = [][]int{ + []int{2, 4, 5}, + []int{4, 2, 3, 6, 5, 3}, + } + } + for _, s := range sos { + fmt.Println("Input: @valuables =", s) + ch := make(chan []int) + go func() { + rob([]int{s[0]}, s[1:], ch) + close(ch) + }() + var max int + var rob []int + for r := range ch { + //fmt.Println(r) + a := sum(r) + if a > max { + max = a + rob = r + } + } + if debug { + fmt.Println(rob) + } + fmt.Printf("Output: %d\n\n", max) + } +} +func rob(c []int, e []int, ch chan<- []int) { + if len(e) < 2 { + //fmt.Println("=>", c) + s := make([]int, len(c)) + copy(s, c) + ch <- s + } else { + for i := 1; i <= 2; i++ { + if len(e[i:]) < 1 { + break + } + rob(append(c, e[i]), e[i+1:], ch) + } + } +} + +func argInts() (s []int) { + if len(os.Args) > 1 { + for _, v := range os.Args[1:] { + i, err := strconv.Atoi(v) + if err != nil { + log.Fatal(err) + } + s = append(s, i) + } + } + return s +} + +func sum(s []int) (r int) { + for _, v := range s { + r += v + } + return r +} |
