-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day8.kt
72 lines (66 loc) · 1.85 KB
/
Day8.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
package aoc2018.day08
import java.io.BufferedReader
import java.io.FileReader
import java.io.IOException
import java.util.*
import java.util.stream.IntStream
import java.util.stream.Stream
fun sumMetadata(inputFile: String): Long {
val input = readInput(inputFile)
return if (input != null) {
val res = parse(input)
res.total.toLong()
} else {
-1
}
}
fun getRootScore(inputFile: String): Long {
val input = readInput(inputFile)
return if (input != null) {
val res = parse(input)
res.score.toLong()
} else {
-1
}
}
private fun parse(input: IntArray): Result {
val children = input[0]
val metas = input[1]
var input2 = Arrays.copyOfRange(input, 2, input.size)
val scores: MutableList<Int> = ArrayList()
var totals = 0
for (i in 0 until children) {
val res = parse(input2)
totals += res.total
scores.add(res.score)
input2 = res.data
}
val currMetas = Arrays.copyOfRange(input2, 0, metas)
val restOfInput = Arrays.copyOfRange(input2, metas, input2.size)
totals += currMetas.sum()
return if (children == 0) {
Result(totals, currMetas.sum(), restOfInput)
} else {
var sc = 0
for (j in currMetas) {
if (j > 0 && j <= scores.size) {
sc += scores[j - 1]
}
}
Result(totals, sc, restOfInput)
}
}
private fun readInput(inputFile: String): IntArray? {
val reader: BufferedReader
return try {
reader = BufferedReader(FileReader(inputFile))
val line = reader.readLine()
val input = line.split(" ").toTypedArray().map { it.toInt() }.toIntArray()
reader.close()
input
} catch (e: IOException) {
e.printStackTrace()
null
}
}
class Result(val total: Int, val score: Int, val data: IntArray)