-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCodeVsZombies.kt
183 lines (165 loc) · 6.99 KB
/
CodeVsZombies.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import java.util.*
import kotlin.math.pow
import kotlin.math.sqrt
/**
* Save humans, destroy zombies!
**/
class CodeVsZombies(val scanner: Scanner) {
fun read(): GameState {
val player = Player(
Coordinate(
x = scanner.nextInt(),
y = scanner.nextInt()
)
)
val humanCount = scanner.nextInt()
val humans = (0 until humanCount).map {
NPC(
id = scanner.nextInt(),
coordinate = Coordinate(
x = scanner.nextInt(),
y = scanner.nextInt()
)
)
}
val zombieCount = scanner.nextInt()
val zombies = (0 until zombieCount).map {
Zombie(
id = scanner.nextInt(),
coordinate = Coordinate(
x = scanner.nextInt(),
y = scanner.nextInt()
),
nextCoordinate = Coordinate(
x = scanner.nextInt(),
y = scanner.nextInt()
)
)
}
return GameState(player, humans, zombies)
}
fun GameState.run(): Coordinate {
val allHumans = humans + player
val distances = Distances(zombies, allHumans)
// calculate which human is targeted by each zombie
val zombieTargets = zombies.map { zombie ->
zombie to (distances.getAllFor(zombie)
.minBy { (_, distance) -> distance }
?: throw IllegalStateException())
}.toMap()
System.err.println("zombieTargets: $zombieTargets")
assert(zombieTargets.keys.containsAll(zombies))
val zombiesTargeting = allHumans.map { human ->
human to zombieTargets
.filterValues { (target, _) -> human == target }
.keys
}.toMap()
.filterValues { zombies -> zombies.isNotEmpty() }
System.err.println("zombiesTargeting: $zombiesTargeting")
// calculate lost humans
val closestApproachingZombie = zombiesTargeting.map { (human, zombies) ->
human to (zombies.minBy { zombie -> distances.getFor(zombie, human) } ?: throw IllegalStateException())
}.toMap()
System.err.println("closestApproaching: $closestApproachingZombie")
val lost = closestApproachingZombie
.filter { (human, _) -> human != player }
.filter { (human, zombie) ->
System.err.println(
"islost: ${distances.getFor(zombie, human)}(${
distances.getFor(zombie, human) / 400
}) vs ${distances.getForPlayer(human)}-2000(${(distances.getForPlayer(human) - 2000) / 1000}) -> ${
distances.getFor(zombie, human) / 400 < (distances.getForPlayer(human) - 2000) / 1000
}"
)
distances.getFor(zombie, human) / zombieStepSize < (distances.getForPlayer(human) - playerKillRange) / playerStepSize
}
System.err.println("all zombies $zombies")
System.err.println("lost $lost")
val target = zombiesTargeting
.filterKeys { human -> human != player }
.filterValues { zombies -> zombies.isNotEmpty() }
.filterKeys { human -> human !in lost.keys }
.keys.firstOrNull()
?: humans.minBy { human -> distances.getForPlayer(human) }
?: throw IllegalStateException()
System.err.println("going towards: $target (${distances.getForPlayer(target)}")
if (distances.getForPlayer(target) < zombieStepSize) {
// check if more points can be made by approaching zombies
val closeZombies = zombies.filter { zombie ->
distances.getForPlayer(zombie) < playerStepSize + playerKillRange
}
if (closeZombies.isNotEmpty()) {
return closeZombies.sortedByDescending(distances::getForPlayer).first().nextCoordinate
}
}
return target.coordinate
}
fun Coordinate.print() {
println(this)
}
class Game {
val xMax = 16000 // width
val yMax = 9000 // height
}
interface HasCoordinate {
val coordinate: Coordinate
fun distance(to: HasCoordinate) = coordinate.distance(to.coordinate)
fun distance(to: Coordinate) = coordinate.distance(to)
}
interface Human : HasCoordinate {
val id: Int
}
data class GameState(val player: Player, val humans: List<NPC>, val zombies: List<Zombie>)
data class NPC(override val id: Int, override val coordinate: Coordinate) : Human
data class Player(override val coordinate: Coordinate) : Human {
override val id = -1
}
data class Zombie(val id: Int, override val coordinate: Coordinate, val nextCoordinate: Coordinate) : HasCoordinate
data class Coordinate(val x: Int, val y: Int) {
override fun toString(): String {
return "$x $y"
}
fun distance(to: Coordinate): Int = sqrt((to.x - this.x).pow(2) + (to.y - this.y).pow(2)).toInt()
}
class Distances(zombies: List<Zombie>, humans: List<Human>) {
private val player = humans.single { it.id == -1 }
private val distances = zombies.flatMap { zombie ->
humans.map { human ->
Pair(zombie, human) to zombie.distance(human)
}
}.toMap()
private val distancesFromZombies = zombies.associateWith { target ->
distances.filterKeys { (zombie, _) -> zombie == target }
.mapKeys { (zombieHumanPair, _) -> zombieHumanPair.let { (_, human) -> human } }
.toMap()
}
private val distancesFromHumans = humans.associateWith { target ->
distances.filterKeys { (_, human) -> human == target }
.mapKeys { (zombieHumanPair, _) -> zombieHumanPair.let { (zombie, _) -> zombie } }
.toMap()
}
private val humanDistancesFromPlayer = humans.associateWith { human -> player.distance(human) }
fun getAll() = distances
fun getFor(zombie: Zombie, human: Human) = distances[Pair(zombie, human)] ?: throw IllegalStateException()
fun getAllFor(target: Zombie) = distancesFromZombies[target] ?: throw IllegalStateException()
fun getAllFor(target: Human) = distancesFromHumans[target] ?: throw IllegalStateException()
fun getForPlayer(human: Human): Int = humanDistancesFromPlayer[human] ?: throw IllegalStateException()
fun getForPlayer(zombie: Zombie): Int = getFor(zombie, player)
}
companion object {
const val playerStepSize = 1000
const val zombieStepSize = 400
const val playerKillRange = 2000
}
}
private fun Int.pow(x: Int): Double = this.toDouble().pow(x)
fun main(args: Array<String>) {
val codeVsZombies = CodeVsZombies(Scanner(System.`in`))
while (true) {
with(codeVsZombies) {
this.read()
.run()
.print()
}
}
}