Skip to content

Instantly share code, notes, and snippets.

@thoroc
Last active August 29, 2015 14:15
Show Gist options
  • Save thoroc/01d13d2297a274cf30c6 to your computer and use it in GitHub Desktop.
Save thoroc/01d13d2297a274cf30c6 to your computer and use it in GitHub Desktop.
codingame - Skynet: the Chasm
package main
import (
"fmt"
"os"
"strconv"
)
type Bike struct {
// speed of the bike
Speed int
// position on the road
Position int
// length of road before gap
Road int
// instruction
Order int
// gap size
Gap int
// platform length
Platform int
// turn
Turn int
}
// command
var C map[int]string
func main() {
// R: the length of the road before the gap.
var R int
fmt.Scan(&R)
// G: the length of the gap.
var G int
fmt.Scan(&G)
// L: the length of the landing platform.
var L int
fmt.Scan(&L)
// S: the motorbike's speed.
var S int
// X: the position on the road of the motorbike.
var X int
C = map[int]string{
-1: "SLOW",
0: "WAIT",
1: "SPEED",
2: "JUMP",
}
// initialize the Terminator
terminator := Bike{S, X, R, 1, G, L, 1}
// game loop
for {
// update the speed
fmt.Scan(&S)
terminator.Speed = S
// update the position
fmt.Scan(&X)
terminator.Position = X
terminator.Update()
// fmt.Fprintln(os.Stderr, "Debug messages...")
fmt.Println(C[terminator.Order]) // A single line containing one of 4 keywords: SPEED, SLOW, JUMP, WAIT.
}
}
func (b *Bike) Update() {
// update the road length before the gap
if b.Turn > 1 {
b.Road = b.Road - b.Speed
}
b.Turn++
fmt.Fprintln(os.Stderr, "ROAD: " + strconv.Itoa(b.Road-1))
fmt.Fprintln(os.Stderr, "GAP: " + strconv.Itoa(b.Gap))
/**
* if the remaining road a multiple of gap
* if the remaining road is greater than the speed -> speed
* if the remaining road is equals to 1 -> jump
* if the gap is behind us -> slow
*/
lGap := b.Gap+1
if b.Road > 0 {
if b.Road < lGap {
b.Order = 2
} else {
if b.Speed < lGap {
b.Order = 1
} else if b.Speed == lGap {
b.Order = 0
} else {
b.Order = -1
}
}
} else {
b.Order = -1 // slow down after the jump
}
fmt.Fprintln(os.Stderr, "SPEED: " + strconv.Itoa(b.Speed) + "; POSITION: " + strconv.Itoa(b.Position))
fmt.Fprintln(os.Stderr, "ORDER: [" + strconv.Itoa(b.Order) + "] => " + C[b.Order])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment