Skip to content

Instantly share code, notes, and snippets.

@mskashi
Created February 22, 2022 23:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mskashi/bc777ee82dfbdf4556954f6df4fedb5e to your computer and use it in GitHub Desktop.
Save mskashi/bc777ee82dfbdf4556954f6df4fedb5e to your computer and use it in GitHub Desktop.
count number of possible positions after three moves in the game of go
import std/sequtils
import std/strutils
import std/tables
type board = array[1..19, array[1..19, char]]
var table = initTable[string, int]()
var count:int = 0
proc print(a:board) =
for i in 1..19:
for j in 1..19:
stdout.write a[i][j]
echo ""
echo ""
proc rotate(a: board) : board =
var r: board
for i in 1..19:
for j in 1..19:
r[i][j] = a[j][20-i]
return r
proc revert(a: board) : board =
var r: board
for i in 1..19:
for j in 1..19:
r[i][j] = a[i][20-j]
return r
proc to_string(a:board) :string = a.mapIt(it.join("")).join("")
proc to_string_dic(a:board) :string =
var tmp:board = a
var s:string
var r:string = to_string(tmp)
tmp = rotate(tmp)
for i in 1..3:
s = to_string(tmp)
if s > r : r = s
tmp = rotate(tmp)
tmp = revert(tmp)
for i in 1..4:
s = to_string(tmp)
if s > r : r = s
tmp = rotate(tmp)
return r
proc solve(x: var board, m: int, n: int) =
if n == m:
var s = to_string_dic(x)
if not table.haskey(s):
print(x)
table[s] = 1
count += 1
echo count
else:
var c: char
if n mod 2 == 0: c = 'O'
else: c = 'X'
for i in 1..19:
for j in 1..19:
if x[i][j] != '.' : continue
x[i][j] = c
solve(x, m, n+1)
x[i][j] = '.'
var x: board
for i in 1..19:
for j in 1..19:
x[i][j] = '.'
solve(x, 3, 0)
echo table.len
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment