forked from kotlin-hands-on/advent-of-code-kotlin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.kt
67 lines (53 loc) · 1.72 KB
/
Utils.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
import java.io.File
import java.math.BigInteger
import java.security.MessageDigest
fun getTestData(name: String) = File("src/mock_data", "$name.txt").readLines()
fun getData(name: String) = File("src/data", "$name.txt").readLines()
open class CodeRunner(private val day: String) {
open val part1Result: Long = -1
open val part2Result: Long = -1
open fun part1(input: List<String>): Int {
TODO("Not yet implemented")
}
open fun part2(input: List<String>): Int {
TODO("Not yet implemented")
}
init {
run()
}
private fun run() {
// test if implementation meets criteria from the description, like:
val testInput = getMockData(day)
try {
check(part1(testInput).toLong() == part1Result)
} catch (ex: NotImplementedError) {
// part 1 not implemented yet
}
try {
check(part2(testInput).toLong() == part2Result)
} catch (ex: NotImplementedError) {
// part 2 not implemented yet
}
val input = getData(day)
try {
println(part1(input))
} catch (ex: NotImplementedError) {
// part 1 not implemented yet
}
try {
println(part2(input))
} catch (ex: NotImplementedError) {
// part 2 not implemented yet
}
}
// To allow easily passing in other data
open fun getMockData(name: String) = getTestData(day)
}
/**
* Reads lines from the given input txt file.
*/
fun readInput(name: String) = File("src", "$name.txt").readLines()
/**
* Converts string to md5 hash.
*/
fun String.md5(): String = BigInteger(1, MessageDigest.getInstance("MD5").digest(toByteArray())).toString(16)