aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpok <pok@ailouros.nemeses.net>2025-08-19 14:05:53 +0700
committerpok <pok@ailouros.nemeses.net>2025-08-19 14:38:42 +0700
commitc1ac9d2e4ac0055d01ca625652003464761cfe38 (patch)
tree18db666472c01255558e568b45b617da85d2646c
parenta25424298927e69f5ab123c6dbc02086b4de67f9 (diff)
downloadperlweeklychallenge-club-c1ac9d2e4ac0055d01ca625652003464761cfe38.tar.gz
perlweeklychallenge-club-c1ac9d2e4ac0055d01ca625652003464761cfe38.tar.bz2
perlweeklychallenge-club-c1ac9d2e4ac0055d01ca625652003464761cfe38.zip
pwc335 solution in go
-rw-r--r--challenge-335/pokgopun/go/ch-1.go92
-rw-r--r--challenge-335/pokgopun/go/ch-2.go169
2 files changed, 261 insertions, 0 deletions
diff --git a/challenge-335/pokgopun/go/ch-1.go b/challenge-335/pokgopun/go/ch-1.go
new file mode 100644
index 0000000000..31a43405b6
--- /dev/null
+++ b/challenge-335/pokgopun/go/ch-1.go
@@ -0,0 +1,92 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-335/
+/*#
+
+Task 1: Common Characters
+
+Submitted by: [41]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given an array of words.
+
+ Write a script to return all characters that is in every word in the
+ given array including duplicates.
+
+Example 1
+
+Input: @words = ("bella", "label", "roller")
+Output: ("e", "l", "l")
+
+Example 2
+
+Input: @words = ("cool", "lock", "cook")
+Output: ("c", "o")
+
+Example 3
+
+Input: @words = ("hello", "world", "pole")
+Output: ("l", "o")
+
+Example 4
+
+Input: @words = ("abc", "def", "ghi")
+Output: ()
+
+Example 5
+
+Input: @words = ("aab", "aac", "aaa")
+Output: ("a", "a")
+
+Task 2: Find Winner
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "github.com/google/go-cmp/cmp"
+ "io"
+ "os"
+ "slices"
+)
+
+func cc(words []string) []rune {
+ res := []rune{}
+ rmn := [][]rune{}
+ for _, v := range words[1:] {
+ rmn = append(rmn, []rune(v))
+ }
+ l := len(rmn)
+ for _, v := range words[0] {
+ i := 0
+ for i < l {
+ j := slices.Index(rmn[i], v)
+ if j < 0 {
+ break
+ }
+ copy(rmn[i][j:], rmn[i][j+1:])
+ rmn[i] = rmn[i][:len(rmn[i])-1]
+ i++
+ }
+ if i == l {
+ res = append(res, v)
+ }
+ }
+ return res
+}
+
+func main() {
+ for _, data := range []struct {
+ input []string
+ output []rune
+ }{
+ {[]string{"bella", "label", "roller"}, []rune{'e', 'l', 'l'}},
+ {[]string{"cool", "lock", "cook"}, []rune{'c', 'o'}},
+ {[]string{"hello", "world", "pole"}, []rune{'l', 'o'}},
+ {[]string{"abc", "def", "ghi"}, []rune{}},
+ {[]string{"aab", "aac", "aaa"}, []rune{'a', 'a'}},
+ {[]string{"กาลก่อน", "กรรมตามทันมาก", "กากี"}, []rune{'ก', 'า', 'ก'}},
+ {[]string{"รอทัก", "รับกรรม", "ปรปักษ์"}, []rune{'ร', 'ั', 'ก'}},
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(cc(data.input), data.output)) // blank if ok, otherwise show the difference
+ }
+}
diff --git a/challenge-335/pokgopun/go/ch-2.go b/challenge-335/pokgopun/go/ch-2.go
new file mode 100644
index 0000000000..d6c41f0729
--- /dev/null
+++ b/challenge-335/pokgopun/go/ch-2.go
@@ -0,0 +1,169 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-335/
+/*#
+
+Task 2: Find Winner
+
+Submitted by: [42]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given an array of all moves by the two players.
+
+ Write a script to find the winner of the TicTacToe game if found based
+ on the moves provided in the given array.
+
+Example 1
+
+Input: @moves = ([0,0],[2,0],[1,1],[2,1],[2,2])
+Output: A
+
+Game Board:
+[ A _ _ ]
+[ B A B ]
+[ _ _ A ]
+
+Example 2
+
+Input: @moves = ([0,0],[1,1],[0,1],[0,2],[1,0],[2,0])
+Output: B
+
+Game Board:
+[ A A B ]
+[ A B _ ]
+[ B _ _ ]
+
+Example 3
+
+Input: @moves = ([0,0],[1,1],[2,0],[1,0],[1,2],[2,1],[0,1],[0,2],[2,2])
+Output: Draw
+
+Game Board:
+[ A A B ]
+[ B B A ]
+[ A B A ]
+
+Example 4
+
+Input: @moves = ([0,0],[1,1])
+Output: Pending
+
+Game Board:
+[ A _ _ ]
+[ _ B _ ]
+[ _ _ _ ]
+
+Example 5
+
+Input: @moves = ([1,1],[0,0],[2,2],[0,1],[1,0],[0,2])
+Output: B
+
+Game Board:
+[ B B B ]
+[ A A _ ]
+[ _ _ A ]
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 25th August
+ 2025.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "github.com/google/go-cmp/cmp"
+ "io"
+ "os"
+)
+
+type Result int
+
+const (
+ A Result = iota
+ B
+ Draw
+ Pending
+ InputError
+)
+
+type Move struct {
+ x, y int
+}
+
+type Moves []Move
+
+func (ms Moves) Process() Result {
+ m2p := make(map[Move]int)
+ for i, v := range ms {
+ for _, e := range []int{v.x,v.y}{
+ if e < 0 || e > 2 {
+ return InputError
+ }
+ }
+ if i%2 == 0 {
+ m2p[v]++
+ } else {
+ m2p[v]--
+ }
+ }
+ if len(ms) != len(m2p) {
+ return InputError
+ }
+ res := Draw
+ type score []int
+ var scores []score
+ var scoredd, scoreda score
+ for i := range 3 {
+ var scoreh, scorev score
+ for e := range 3 {
+ scoreh = append(scoreh, m2p[Move{e, i}])
+ scorev = append(scorev, m2p[Move{i, e}])
+ }
+ scores = append(scores, scoreh, scorev)
+ scoredd = append(scoredd, m2p[Move{i, i}])
+ scoreda = append(scoreda, m2p[Move{i, 2 - i}])
+ }
+ for _, v := range append(scores, scoredd, scoreda) {
+ var c, s int
+ for _, p := range v {
+ if p != 0 {
+ c++
+ s += p
+ }
+ }
+ if c < 2 {
+ res = Pending
+ continue
+ }
+ switch s {
+ case 3:
+ return A
+ case -3:
+ return B
+ case 2, -2:
+ res = Pending
+ }
+ }
+ return res
+}
+
+func main() {
+ for _, data := range []struct {
+ input Moves
+ output Result
+ }{
+ {Moves{Move{0, 0}, Move{2, 0}, Move{1, 1}, Move{2, 1}, Move{2, 2}}, A},
+ {Moves{Move{0, 0}, Move{1, 1}, Move{0, 1}, Move{0, 2}, Move{1, 0}, Move{2, 0}}, B},
+ {Moves{Move{0, 0}, Move{1, 1}, Move{2, 0}, Move{1, 0}, Move{1, 2}, Move{2, 1}, Move{0, 1}, Move{0, 2}, Move{2, 2}}, Draw},
+ {Moves{Move{0, 0}, Move{1, 1}}, Pending},
+ {Moves{Move{1, 1}, Move{0, 0}, Move{2, 2}, Move{0, 1}, Move{1, 0}, Move{0, 2}}, B},
+ {Moves{Move{1, 1}, Move{0, 0}, Move{1, 1}, Move{0, 1}, Move{1, 0}, Move{0, 2}}, InputError},
+ {Moves{Move{1, 1}, Move{0, 0}, Move{2, 2}, Move{2, 2}, Move{1, 0}, Move{0, 2}}, InputError},
+ {Moves{Move{1, 1}, Move{0, 0}, Move{2, 2}, Move{3, 1}, Move{1, 0}, Move{0, 2}}, InputError},
+ {Moves{Move{1, 1}, Move{0, 0}, Move{2, 2}, Move{0, 1}, Move{1, -1}, Move{0, 2}}, InputError},
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(data.input.Process(), data.output)) // blank if ok, otherwise show the difference
+ }
+}