-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day07.kt
50 lines (39 loc) · 1.27 KB
/
Day07.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
package y2021
import general.Day
import kotlin.math.abs
object Day07 : Day() {
override val name = "The Treachery of Whales"
override fun a1() {
val input = INPUT.readText()
.split(",")
.map { it.toInt() }
.sorted()
val median = input[input.size / 2]
val fuel = input.sumOf { abs(it - median) }
println(fuel)
}
override fun a2() {
val input = INPUT.readText().split(",").map { it.toInt() }
var fuel = Integer.MAX_VALUE
var prev = Integer.MAX_VALUE
var pos = input.sum() / input.size // approx guess, near correct position
var direction = 1
while (true) {
val distance = input.sumOf {
val n = abs(it - pos)
// n*(n+1)/2 = sum(1+...+n)
n * (n + 1) / 2
}
if (distance < fuel) fuel = distance
if (distance > prev) {
if (direction == -1) break
direction = -1 // search into other direction
pos -= 2 // skip previous calculation of fuel (=prev)
continue // prevent override: prev = distance
}
pos += direction
prev = distance
}
println(fuel)
}
}