xuannhat25
BAN USERpackage main
// Find target in chess board with given start position of knight
import (
"fmt"
)
// kight goes as L shape
//
// Up/Down first
// x,y x+1,y
// x,y-1 x+1,y-1
// x,y-2 x+1,y-2
// Left/Right first
// x,y x+1,y x+2,y
// x,y-1 x+1,y-1 x+2,y-1
// chess board is 64 * 64 square (0,0) (0,7) (7, 0) (7,7)
const (
MinX = 0
MinY = 0
MaxX = 7
MaxY = 7
)
type Point struct {
X int
Y int
}
func main() {
moveFuncs := []MoveFunc{
moveUpLeft,
moveUpRight,
moveDownLeft,
moveDownRight,
moveLeftUp,
moveLeftDown,
moveRightUp,
moveRightDown,
}
src := Point{7, 7}
possibleDests := []Point{}
for _, moveFunc := range moveFuncs {
moveFn := moveFunc
if dest, canMove := moveFn(src); canMove {
possibleDests = append(possibleDests, dest)
}
}
fmt.Println(possibleDests)
}
type MoveFunc func(src Point) (dest Point, canMove bool)
func validPoint(p Point) bool {
return p.X >= MinX && p.X <= MaxX && p.Y >= MinY && p.Y <= MaxY
}
// x+1,y-2 to x,y
func moveUpLeft(src Point) (dest Point, canMove bool) {
dest = Point{
X: src.X - 1,
Y: src.Y + 2,
}
return dest, validPoint(dest)
}
// x,y-2 to x+1,y
func moveUpRight(src Point) (dest Point, canMove bool) {
dest = Point{
X: src.X + 1,
Y: src.Y + 2,
}
return dest, validPoint(dest)
}
// x+1,y to x,y-2
func moveDownLeft(src Point) (dest Point, canMove bool) {
dest = Point{
X: src.X - 1,
Y: src.Y - 2,
}
return dest, validPoint(dest)
}
// x,y to x+1,y-2
func moveDownRight(src Point) (dest Point, canMove bool) {
dest = Point{
X: src.X + 1,
Y: src.Y - 2,
}
return dest, validPoint(dest)
}
// x+2,y-1 to x,y
func moveLeftUp(src Point) (dest Point, canMove bool) {
dest = Point{
X: src.X - 2,
Y: src.Y + 1,
}
return dest, validPoint(dest)
}
// x+2,y to x,y-1
func moveLeftDown(src Point) (dest Point, canMove bool) {
dest = Point{
X: src.X - 2,
Y: src.Y - 1,
}
return dest, validPoint(dest)
}
// x,y-1 to x+2, y
func moveRightUp(src Point) (dest Point, canMove bool) {
dest = Point{
X: src.X + 2,
Y: src.Y + 1,
}
return dest, validPoint(dest)
}
// x,y to x+2,y-1
func moveRightDown(src Point) (dest Point, canMove bool) {
dest = Point{
X: src.X + 2,
Y: src.Y - 1,
}
return dest, validPoint(dest)
}
// Implement binary addition of two strings.
// For example "101101" and "111101" equal "1101010"
// You cannot use any type conversion, operate only with strings.
// "1" + "0" = "1"
// "0" + "0" = "0"
// "1" + "1" = "0" reminder-1
//
// "1" + "0" + reminder-1 = "0" reminder-1
// "1" + "1" + reminder-1 = "1" reminder-1
// "0" + "0" + reminder-1 = "1"
//
// first + second = (first(0..n-1) + second(0..m-1) + reminder(first(n) + second(m)) append (first(n) + second(m))
//
// "101101" + "111101" = ("10110" + "11110" + reminder-1) append 0
func StringBinaryAdd(first string, second string) string {
return StringBinaryAddRecursive(first, second, "0")
}
func StringBinaryAddRecursive(first string, second string, currentReminder string) string {
if len(first) == 0 && len(second) == 0 {
if currentReminder == "1" {
return "1"
}
return ""
}
if len(first) == 0 {
return StringBinaryAddRecursive(second, currentReminder, "0")
}
if len(second) == 0 {
return StringBinaryAddRecursive(first, currentReminder, "0")
}
lastFirstIndex := len(first)-1
lastSecondIndex := len(second)-1
result, lastIndexReminder := addBinaryStringSingleDigit(string(first[lastFirstIndex]), string(second[lastSecondIndex]))
result, tempReminder := addBinaryStringSingleDigit(result, currentReminder)
finalReminder, _ := addBinaryStringSingleDigit(lastIndexReminder, tempReminder)
return StringBinaryAddRecursive(first[:lastFirstIndex], second[:lastSecondIndex], finalReminder) + result
}
func addBinaryStringSingleDigit(firstDigit string, secondDigit string) (result string, reminder string) {
if firstDigit == "0" {
if secondDigit == "0" {
return "0", "0"
}
return "1", "0"
} else {
if secondDigit == "0" {
return "1", "0"
}
return "0", "1"
}
}
- xuannhat25 March 23, 2021