Skip to content

Instantly share code, notes, and snippets.

@ganzuul
Last active November 25, 2018 21:59
Show Gist options
  • Save ganzuul/5f701f666b8ea06a00d8158c4a237147 to your computer and use it in GitHub Desktop.
Save ganzuul/5f701f666b8ea06a00d8158c4a237147 to your computer and use it in GitHub Desktop.
Multiplies config files using a range of lines
// Usage: java kotgombinate.jar Dir1 Dir2 Dir3 startOfRange endOfRange
// Output: Dir1(A, B) * Dir2(C, D) = Dir3 (A_C, A_D, B_C, B_D)
// Range is line-numbers so files in Dir1 and Dir2 need to correspond 1-to-1
import java.io.File
import kotlin.system.exitProcess
data class Butt(var fileContents: MutableList<List<String>>, var fileName: MutableList<String>)
var dir1 = Butt(ArrayList(), ArrayList())
var dir2 = Butt(ArrayList(), ArrayList())
var dir3 = Butt(ArrayList(), ArrayList())
var a: MutableList<Int> = ArrayList()
var range = Array(0, {0})
var out = ""
const val notInRange: String = "Input range in format a b e.g. 10 123"
fun main(args: Array<String>) {
// for (e in args) {println(e)}
if (args.isNotEmpty() ) {
dir1 = readDir(args[0])
dir2 = readDir(args[1])
dir3 = readDir(args[2])
a.add(args[3].toInt())
a.add(args[4].toInt())
}
else{
println("Null argument")
exitProcess(0)
}
if (a[0] > a[1]) {
println(notInRange)
exitProcess(0)
}
range = Array(a[1] - a[0]) {i -> (i + a[0])}
println("Range length: ${range.size}")
var i = 0
var o = 0
dir2.fileName.forEach{
val it2 = it
var j = 0
var g = 0
var k:Int
dir1.fileName.forEach() {
dir3.fileName.add(it2 + " " + it)
while (j < a[0]) {
//println("Dir2: $j")
out += dir2.fileContents[i][j] + "\n"
j++
}
k = a[1] + 1
for (e in range) {
//println("Dir1: $e")
out += dir1.fileContents[g][e] + "\n"
}
while (k < dir2.fileContents[i].size) {
//println("Dir2: $k")
out += dir2.fileContents[i][k] + "\n"
k++
}
j = 0
k = 0
println("Dir3: ${dir3.fileName}")
println("No. Dir1 files $g")
File("./Dir3/${dir3.fileName[o]}.bc").writeText(out)
o++
g++
out = ""
}
println("No. Dir2 files $i")
i++
}
}
fun readDir(dir: String): Butt {
val data = Butt(ArrayList(), ArrayList())
File(dir).walkTopDown().forEach {
if (!it.isDirectory)
data.fileName.add(it.toString().substringAfterLast("/").substringBeforeLast("."))
if (File(it.toString()).isFile)
data.fileContents.add(File(it.toString()).readLines()) //add list of lines to list of lists
}
return data
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment