package aoc.kt.y2018;

/**
 * Day 5.
 */

/** Part 1 */
fun processPolymer1(input: String): String {
    val output = input.toCharArray()
    .forEachIndexed { i, c ->
        if (i != 0) {
            var reacting = true
            var range = 0..0
            while (reacting) {
                var offset = 0
                if (reactionOccurs(c, input.get(-1))) {
                } else {
                    reacting = false
                }
            }
        }
    }
    return output.toString()
}

/** Part 2 */
fun processPolymer2(input: String): String {
    return "42"
}

fun reactionOccurs(char: Char, prev: Char): Boolean {
    return false
}

package aoc.kt.y2018;

/**
 * Day 5.
 */

/** Part 1 */
fun processPolymer1(input: String): String {
    var polymer = Pair(input, true)
    while (polymer.second) {
        polymer = react(polymer.first)
    }

    return polymer
        //.first.length
        .toString()
}

/** Part 2 */
fun processPolymer2(input: String): String {
    return "42"
}

fun react(input: String): Pair<String, Boolean> {
    var result = mutableListOf<Char>()
    var polymer = input.toMutableList()
    var reactionOccured = false

    while (polymer.next() != null) {
        polymer.dequeue()?.let { a ->
            if (polymer.next() != null) {
                polymer.dequeue()?.let { b ->
                    if (a.equals(b, true)) {
                        reactionOccured = true
                    } else {
                        result.push(a)
                        polymer.enqueue(b)
                    }
                }
            }
        }
    }

    val resultStr: String = result.map { it.toString() }.reduce { acc, n -> acc + n }
    return Pair(resultStr, reactionOccured)
}

fun <T> MutableList<T>.push(e: T) {
    this.add(e)
}

fun <T> MutableList<T>.dequeue(): T? {
    if (this.isNotEmpty()) {
        return this.removeAt(0)
    } else {
        return null
    }
}

fun <T> MutableList<T>.enqueue(e: T) {
    this.add(0, e)
}

fun <T> MutableList<T>.next(): T? {
    return this.getOrNull(0)
}

Update with new approach to day 5
Update with new approach to day 5

nathanjent/adventofcode-rust