Skip to content

Instantly share code, notes, and snippets.

@thoroc
Created February 16, 2015 23:36
Show Gist options
  • Select an option

  • Save thoroc/b8fd24e46719487e4d7b to your computer and use it in GitHub Desktop.

Select an option

Save thoroc/b8fd24e46719487e4d7b to your computer and use it in GitHub Desktop.
codingame - Power of Thor
package main
import (
"fmt"
"bytes"
"os"
"strconv"
)
func main() {
// LX: the X position of the light of power
// LY: the Y position of the light of power
// TX: Thor's starting X position
// TY: Thor's starting Y position
var LX, LY, TX, TY int
fmt.Scan(&LX, &LY, &TX, &TY)
var thor_pos = Position{TX, TY}
var light_pos = Position{LX, LY}
var direction string
for {
thor_pos.Update(direction)
fmt.Fprintln(os.Stderr, thor_pos.pos_x)
fmt.Fprintln(os.Stderr, thor_pos.pos_y)
// E: The level of Thor's remaining energy, representing the number of moves he can still make.
var E int
fmt.Scan(&E)
direction = thor_pos.Direction(light_pos)
// A single line providing the move to be made: N NE E SE S SW W or NW
fmt.Println(direction)
}
}
type Position struct {
pos_x, pos_y int
}
func (o *Position) Update(dir string) {
if dir == "N" {
o.pos_y--
} else if dir == "NE" {
o.pos_x++
o.pos_y--
} else if dir == "E" {
o.pos_x++
} else if dir == "SE" {
o.pos_x++
o.pos_y++
} else if dir == "S" {
o.pos_y++
} else if dir == "SW" {
o.pos_x--
o.pos_y++
} else if dir == "W" {
o.pos_x--
} else if dir == "NW" {
o.pos_x--
o.pos_y--
}
fmt.Fprintf(os.Stderr,"updated X: " + strconv.Itoa(o.pos_x) + "\n")
fmt.Fprintf(os.Stderr,"updated Y: " + strconv.Itoa(o.pos_y) + "\n")
}
func (o Position) Direction(d Position) string {
var lat string
var long string
var buffer bytes.Buffer
lat = ""
if o.pos_y > -1 && o.pos_y < 18 {
if o.pos_y > d.pos_y {
lat = "N"
} else if o.pos_y < d.pos_y {
lat = "S"
} else if o.pos_y == d.pos_y {
lat = ""
}
}
fmt.Fprintf(os.Stderr,"North-South: " + lat + "\n")
buffer.WriteString(lat)
long = ""
if o.pos_x > -1 && o.pos_x < 40 {
if o.pos_x > d.pos_x {
long = "W"
} else if o.pos_x < d.pos_x {
long = "E"
} else if o.pos_x == d.pos_x {
long = ""
}
}
fmt.Fprintf(os.Stderr,"East-West: " + long + "\n")
buffer.WriteString(long)
return buffer.String()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment