Last active
September 5, 2015 12:17
-
-
Save junjiah/248f0e99b9b8c6f9885a to your computer and use it in GitHub Desktop.
learning golang in hackerrank AI domain https://www.hackerrank.com/domains/ai
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Solved 'Bot saves princess' on hackerrank | |
// https://www.hackerrank.com/challenges/saveprincess | |
package main | |
import ( | |
"bufio" | |
"fmt" | |
"os" | |
"strings" | |
) | |
func main() { | |
var princessX, princessY int | |
var botX, botY int | |
var n int | |
// Read matrix size. | |
fmt.Scanf("%d", &n) | |
scanner := bufio.NewScanner(os.Stdin) | |
for i := 0; i < n; i++ { | |
scanner.Scan() | |
line := scanner.Text() | |
// Find the princess. | |
if i == 0 || i == n-1 { | |
if idx := strings.Index(line, "p"); idx != -1 { | |
princessX, princessY = idx, i | |
} | |
} | |
// Find the bot. | |
if idx := strings.Index(line, "m"); idx != -1 { | |
botX, botY = idx, i | |
} | |
} | |
// Move the bot. | |
horMove, horDist := "LEFT", botX-princessX | |
if horDist < 0 { | |
horMove = "RIGHT" | |
horDist = -horDist | |
} | |
for ; horDist > 0; horDist-- { | |
fmt.Println(horMove) | |
} | |
vertMove, vertDist := "UP", botY-princessY | |
if vertDist < 0 { | |
vertMove = "DOWN" | |
vertDist = -vertDist | |
} | |
for ; vertDist > 0; vertDist-- { | |
fmt.Println(vertMove) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Solved 'BotClean Large' on hackerrank | |
// https://www.hackerrank.com/challenges/botcleanlarge | |
// Pure greedy algorithm. An improvement would be to calculate | |
// an optimal path. Score: 54.1 | |
package main | |
import ( | |
"bufio" | |
"fmt" | |
"math" | |
"os" | |
) | |
func abs(v int) int { | |
if v < 0 { | |
return -v | |
} else { | |
return v | |
} | |
} | |
func main() { | |
// Postition of nearest dirt and bot. | |
var targetX, targetY int | |
var botX, botY int | |
// Read bot position. | |
fmt.Scanf("%d %d", &botY, &botX) | |
// The distance between the bot and the nearest dirt. | |
minDist := math.MaxInt32 | |
scanner := bufio.NewScanner(os.Stdin) | |
// Ignore the next line of board size. | |
scanner.Scan() | |
for y := 0; scanner.Scan(); y++ { | |
line := scanner.Text() | |
// Find the dirt, and calculate the dist. | |
for x, ch := range line { | |
if ch == 'd' { | |
dist := abs(x-botX) + abs(y-botY) | |
if dist == 0 { | |
// Clean current dirt. | |
fmt.Println("CLEAN") | |
return | |
} else if dist < minDist { | |
minDist, targetX, targetY = dist, x, y | |
} | |
} | |
} | |
} | |
// Move the bot. | |
if botX-targetX > 0 { | |
fmt.Println("LEFT") | |
} else if botX-targetX < 0 { | |
fmt.Println("RIGHT") | |
} else if botY-targetY > 0 { | |
fmt.Println("UP") | |
} else { | |
fmt.Println("DOWN") | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment