-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay5.kt
52 lines (45 loc) · 1.38 KB
/
Day5.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
package aoc2018.day05
import java.io.BufferedReader
import java.io.FileReader
import java.io.IOException
fun computeAlchemicalReduction(inputString: String): String {
var input = inputString
var output = ""
var k = 0
while (k < 100000) {
for (i in 0 until input.length - 1) {
val thisAscii = input[i].toInt()
val nextAscii = input[i + 1].toInt()
if (thisAscii + 32 == nextAscii || thisAscii - 32 == nextAscii) {
output = charRemoveAt(input, i)
output = charRemoveAt(output, i)
input = output
break
}
}
k++
}
return if (output.isNotEmpty()) output else input
}
fun computeAlchemicalReductionLength(input: String): Int {
val output = computeAlchemicalReduction(input)
return output.length
}
fun charRemoveAt(str: String, p: Int): String {
return str.substring(0, p) + str.substring(p + 1)
}
fun readInputFile(filepath: String): String {
val reader: BufferedReader
var line = ""
try {
reader = BufferedReader(FileReader(filepath))
line = reader.readLine()
reader.close()
} catch (e: IOException) {
e.printStackTrace()
}
return line
}
fun removeAllLetter(input: String, letter: String): String {
return input.replace(letter.toLowerCase(), "").replace(letter.toUpperCase(), "")
}