diff options
| author | Michael Manring <michael@manring> | 2024-08-06 18:59:36 +1000 |
|---|---|---|
| committer | Michael Manring <michael@manring> | 2024-08-06 18:59:36 +1000 |
| commit | 8bd7329fb0dec9a57bb33590877839ac69728b82 (patch) | |
| tree | 451fae642de78b42ca449759999bc0a78f532382 | |
| parent | 070329afb38475de9ad40bcadd50ea1672126940 (diff) | |
| download | perlweeklychallenge-club-8bd7329fb0dec9a57bb33590877839ac69728b82.tar.gz perlweeklychallenge-club-8bd7329fb0dec9a57bb33590877839ac69728b82.tar.bz2 perlweeklychallenge-club-8bd7329fb0dec9a57bb33590877839ac69728b82.zip | |
pwc281 solution in go
| -rw-r--r-- | challenge-281/pokgopun/go/ch-1.go | 60 | ||||
| -rw-r--r-- | challenge-281/pokgopun/go/ch-2.go | 109 |
2 files changed, 169 insertions, 0 deletions
diff --git a/challenge-281/pokgopun/go/ch-1.go b/challenge-281/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..7a10b39811 --- /dev/null +++ b/challenge-281/pokgopun/go/ch-1.go @@ -0,0 +1,60 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-281/ +/*# + +Task 1: Check Color + +Submitted by: [47]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given coordinates, a string that represents the coordinates of + a square of the chessboard as shown below: + + Week_281_Task_1 + + Write a script to return true if the square is light, and false if the + square is dark. + +Example 1 + +Input: $coordinates = "d3" +Output: true + +Example 2 + +Input: $coordinates = "g5" +Output: false + +Example 3 + +Input: $coordinates = "e6" +Output: true + +Task 2: Knight’s Move +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + + "github.com/google/go-cmp/cmp" +) + +func checkColor(str string) bool { + return ((str[0] % 2) + (str[1] % 2)) == 1 +} + +func main() { + for _, data := range []struct { + input string + output bool + }{ + {"d3", true}, + {"g5", false}, + {"e6", true}, + } { + io.WriteString(os.Stdout, cmp.Diff(checkColor(data.input), data.output)) // blank if ok, otherwise show the difference + } +} diff --git a/challenge-281/pokgopun/go/ch-2.go b/challenge-281/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..9f4f229646 --- /dev/null +++ b/challenge-281/pokgopun/go/ch-2.go @@ -0,0 +1,109 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-281/ +/*# + +Task 2: Knight’s Move + +Submitted by: [48]Peter Campbell Smith + __________________________________________________________________ + + A Knight in chess can move from its current position to any square two + rows or columns plus one column or row away. So in the diagram below, + if it starts a S, it can move to any of the squares marked E. + + Write a script which takes a starting position and an ending position + and calculates the least number of moves required. + + Week_281_Task_2 + +Example 1 + +Input: $start = 'g2', $end = 'a8' +Ouput: 4 + +g2 -> e3 -> d5 -> c7 -> a8 + +Example 2 + +Input: $start = 'g2', $end = 'h2' +Ouput: 3 + +g2 -> e3 -> f1 -> h2 + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 11th August + 2024. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +#*/ +//# solution by pokgopun@gmail.com +// https://afteracademy.com/blog/knight-on-chessboard/#:~:text=Solution,the%20shape%20of%20an%20L). + +package main + +import ( + "io" + "os" + + "github.com/google/go-cmp/cmp" +) + +type position struct { + x, y int +} + +func newPosition(label string) position { + return position{int(label[0]) - 96, int(label[1]) - 48} +} + +func (pt position) String() string { + return string([]byte{byte(pt.x) + 96, byte(pt.y) + 48}) +} + +type data struct { + count int + visited bool +} + +type board [8][8]data + +func (bd board) contains(p position) bool { + return p.x >= 1 && p.x <= 8 && p.y >= 1 && p.y <= 8 +} + +func knightMove(start, end string) int { + var bd board + p0 := newPosition(start) + p1 := newPosition(end) + queue := []position{p0} + bd[p0.x-1][p0.y-1] = data{visited: true} + dx := [8]int{-2, -1, 1, 2, -2, -1, 1, 2} + dy := [8]int{-1, -2, -2, -1, 1, 2, 2, 1} + for len(queue) > 0 { + p := queue[0] + queue = queue[1:] + if p.x == p1.x && p.y == p1.y { + return bd[p.x-1][p.y-1].count + } + for i := 0; i < 8; i++ { + pn := position{p.x + dx[i], p.y + dy[i]} + if bd.contains(pn) && !bd[pn.x-1][pn.y-1].visited { + bd[pn.x-1][pn.y-1] = data{visited: true, count: bd[p.x-1][p.y-1].count + 1} + queue = append(queue, pn) + } + } + } + return -1 +} + +func main() { + for _, data := range []struct { + start, end string + count int + }{ + {"g2", "a8", 4}, + {"g2", "h2", 3}, + } { + io.WriteString(os.Stdout, cmp.Diff(knightMove(data.start, data.end), data.count)) // blank if ok, otherwise show the difference + } +} |
