-
Notifications
You must be signed in to change notification settings - Fork 0
/
9.kt
67 lines (53 loc) · 2.17 KB
/
9.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
val list: List<String> = File("src/main/resources/dayninetest.txt").useLines { it.toList() }
var count = 0L
fun main() {
val data: MutableList<LongArray> = mutableListOf() // A list of sequences to test
for (line in list) { // Format data
data.add(line.split(" ").mapNotNull { it.toLongOrNull() }.toLongArray())
}
for (i in data.indices) {
print("\n")
var sequence = data[i]
printArray(sequence)
val lastNumbers: MutableList<Long> = mutableListOf() // The last numbers of each sequence step
val firstNumbers: MutableList<Long> = mutableListOf() // the first nums
do {
lastNumbers.add(sequence.last())
firstNumbers.add(sequence.first())
sequence = getDifferences(sequence)
} while (!isLastArray(sequence))
lastNumbers.reverse() // Top-down -> Bottom-up
firstNumbers.reverse()
val extrapolatedFutureNumbers = LongArray(lastNumbers.size)
for (x in lastNumbers.indices) {
val sublist = lastNumbers.subList(0, x + 1)
extrapolatedFutureNumbers[x] = sublist.sum()
}
val extrapolatedPreviousNumbers = LongArray(firstNumbers.size)
for (x in firstNumbers.indices) {
if (x == 0) { extrapolatedPreviousNumbers[0] = firstNumbers[x]; continue }
extrapolatedPreviousNumbers[x] = firstNumbers[x] - extrapolatedPreviousNumbers[x - 1]
}
println("Next number in sequence = ${extrapolatedFutureNumbers.last()}") // Part 1
println("Previous number in sequence = ${extrapolatedPreviousNumbers.last()}") // Part 2
count += extrapolatedPreviousNumbers.last()
}
println(" ")
println("Sum of extrapolated nums: $count")
}
fun getDifferences(nums: LongArray): LongArray {
val array = LongArray(nums.size - 1)
for (i in nums.indices) {
if (i == nums.size - 1) continue
array[i] = nums[i + 1] - nums[i] // diff of nums
}
return array
}
fun isLastArray(nums: LongArray): Boolean {
for (num in nums) if (num != 0L) return false
return true
}
fun printArray(nums: LongArray) {
for (num in nums) print("$num ")
print("\n")
}