Skip to content

Instantly share code, notes, and snippets.

@lilgallon
Created May 7, 2021 12:33
Show Gist options
  • Save lilgallon/2ac59f716f9a9b014afdcb325228d94b to your computer and use it in GitHub Desktop.
Save lilgallon/2ac59f716f9a9b014afdcb325228d94b to your computer and use it in GitHub Desktop.
codingame spring ai
import java.util.*
import java.io.*
import java.math.*
data class Cell (
val index: Int,
val richness: Int,
val neighbours: List<Int>
)
data class Tree (
val cell: Int,
val size: Int,
val isMine: Boolean,
val isDormant: Boolean
)
enum class ActionType (val str: String) {
WAIT("WAIT"),
SEED("SEED"),
GROW("GROW"),
COMPLETE("COMPLETE")
}
class Action (
val type: ActionType,
val source: Int? = null,
val target: Int? = null
) {
companion object {
fun parse(action: String): Action = action
.split(" ")
.let {
when (ActionType.valueOf(it[0])) {
ActionType.WAIT -> return Action(ActionType.WAIT)
ActionType.SEED -> return Action(ActionType.SEED, it[1].toInt(), it[2].toInt())
else -> return Action(ActionType.valueOf(it[0]), it[1].toInt())
}
}
}
override fun toString(): String = when (this.type) {
ActionType.WAIT -> ActionType.WAIT.toString()
ActionType.SEED -> "%s %d %d".format(this.type.toString(), this.source, this.target)
else -> "%s %d".format(this.type.toString(), this.target)
}
}
data class GameContext (
val day: Int,
val nutrients: Int,
val trees: List<Tree>,
val mySun: Int,
val opponentSun: Int,
val myScore: Int,
val opponentScore: Int,
val isOpponentWaiting: Boolean
)
class Game (val cells: List<Cell>) {
var context: GameContext? = null
var possibleActions: List<Action> = emptyList()
fun getNextAction(): Action {
return possibleActions.get(0)
}
}
/**
* Auto-generated code below aims at helping you parse
* the standard input according to the problem statement.
**/
fun main(args : Array<String>) {
val input = Scanner(System.`in`)
val numberOfCells = input.nextInt() // 37
val game = Game(
(0..numberOfCells).map {
val index = input.nextInt() // 0 is the center cell, the next cells spiral outwards
val richness = input.nextInt() // 0 if the cell is unusable, 1-3 for usable cells
val n0 = input.nextInt() // the index of the neighbouring cell for each direction
val n1 = input.nextInt()
val n2 = input.nextInt()
val n3 = input.nextInt()
val n4 = input.nextInt()
val n5 = input.nextInt()
Cell(
index, richness, listOf(n0, n1, n2, n3, n4, n5)
)
}
)
// game loop
while (true) {
val day = input.nextInt() // the game lasts 24 days: 0-23
val nutrients = input.nextInt() // the base score you gain from the next COMPLETE action
val sun = input.nextInt() // your sun points
val score = input.nextInt() // your current score
val oppSun = input.nextInt() // opponent's sun points
val oppScore = input.nextInt() // opponent's score
val oppIsWaiting = input.nextInt() != 0 // whether your opponent is asleep until the next day
val numberOfTrees = input.nextInt() // the current amount of trees
game.context = GameContext(
day = day,
nutrients = nutrients,
trees = (0..numberOfTrees).map {
val cellIndex = input.nextInt() // location of this tree
val size = input.nextInt() // size of this tree: 0-3
val isMine = input.nextInt() != 0 // 1 if this is your tree
val isDormant = input.nextInt() != 0 // 1 if this tree is dormant
Tree(cellIndex, size, isMine, isDormant)
},
mySun = sun,
opponentSun = oppSun,
myScore = score,
opponentScore = oppScore,
isOpponentWaiting = oppIsWaiting
)
val numberOfPossibleActions = input.nextInt() // all legal actions
if (input.hasNextLine()) {
input.nextLine()
}
game.possibleActions = (0..numberOfPossibleActions).map {
Action.parse(input.nextLine())
}
// Write an action using println()
// To debug: System.err.println("Debug messages...");
// GROW cellIdx | SEED sourceIdx targetIdx | COMPLETE cellIdx | WAIT <message>
println(game.getNextAction().toString())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment