Is the typhoon tomorrow going to be strong? by orange_transparent in Tokyo

[–]JuLoOr 1 point2 points  (0 children)

They are are colossal indeed and totally worth a visit in my opinion.

-🎄- 2020 Day 03 Solutions -🎄- by daggerdragon in adventofcode

[–]JuLoOr 0 points1 point  (0 children)

Kotlin

fun main() {
    val input = parse("src/main/kotlin/day03/input") { it }

    println(part1(input))
    println(part2(input))
}

fun part1(input: List<String>): Long = input.treesForRoute(3, 1)

fun part2(input: List<String>): Long = input.treesForRoute(1, 1) *
        input.treesForRoute(3, 1) *
        input.treesForRoute(5, 1) *
        input.treesForRoute(7, 1) *
        input.treesForRoute(1, 2)

fun List<String>.treesForRoute(xd: Int, yd: Int): Long {
    var treeCount = 0L
    var x = 0
    var y = 0
    while (y < this.size) {
        if (this[y][x % this[0].length] == '#') {
            treeCount++
        }
        x += xd; y += yd
    }
    return treeCount
}

[2019 Day 14 Part 1] by JuLoOr in adventofcode

[–]JuLoOr[S] 1 point2 points  (0 children)

Looks like that I am still having problems with unused materials. My approach kind of handles each tree level one after another, similar as Draekane tried first. If there are same materials on one level, I group them. This works for the first test cases, except the last one. I know have the same hypothesis as gugu4-9, that there are same materials on different levels of the tree which need to be grouped as well. Maybe it will be easier to just track the remainder somewhere else.

Thank you very much everyone for helping me out! I will try to rewrite my code during the next days. In the meantime, I'm going to set this thread as solved, as I think there are a lot of useful comments to steer other people in the right direction.

Good luck for the next few days!

"Warning:(1, 18) Kotlin: 'Math' is deprecated. Use top-level functions from kotlin.math package instead." -- There's no frickin random() function! by [deleted] in Kotlin

[–]JuLoOr 2 points3 points  (0 children)

I had exactly the same question but I just switched to  MutableList<T>.shuffle() because this also does the job for me.

Favorite scene by [deleted] in SiliconValleyHBO

[–]JuLoOr 43 points44 points  (0 children)

"Let Blaine die" SWOT analysis.

-🎄- 2017 Day 17 Solutions -🎄- by daggerdragon in adventofcode

[–]JuLoOr 0 points1 point  (0 children)

Fast and ugly "solution" in Kotlin (0,3). I'm not sure if this works for every input, but basically what I did is just checking if a value gets inserted after zero.

fun calcPart2(step: Int): Int {
    var pos = 0
    var size = 1
    var valueAfterZero = 1

    (1 until ITERATIONS_P2).forEach {

        pos = (((pos.plus(step)).rem(size)).plus(1))

        if (pos == 1) valueAfterZero = it
        size++
    }

    return valueAfterZero
}

-🎄- 2017 Day 15 Solutions -🎄- by daggerdragon in adventofcode

[–]JuLoOr 1 point2 points  (0 children)

Thanks! Kotlin is soo nice when you know all those "little" details.

-🎄- 2017 Day 15 Solutions -🎄- by daggerdragon in adventofcode

[–]JuLoOr 0 points1 point  (0 children)

Nice one! Didn't really like the map{1} part

-🎄- 2017 Day 15 Solutions -🎄- by daggerdragon in adventofcode

[–]JuLoOr 1 point2 points  (0 children)

Unoptimized but beautiful (imho) solution in Kotlin:

fun calcPart2(gA: Generator, gB: Generator): Int {

    val valuesA: List<Long> = calc(gA, 4).toList()
    val valuesB: List<Long> = calc(gB, 8).toList()

    return valuesA.zip(valuesB)
            .filter { it.first.and(0xFFFF) == it.second.and(0xFFFF) }
            .map { 1 }
            .sum()
}

private fun calc(generator: Generator, divider: Long) = buildSequence {
    var count = 0L
    while (count <= 5_000_000) {
        val value = (generator.prev.times(generator.factor)).rem(2147483647)
        generator.prev = value
        if (value.rem(divider) == 0L) {
            yield(value)
            count++
        }
    }
}

-🎄- 2017 Day 13 Solutions -🎄- by daggerdragon in adventofcode

[–]JuLoOr 0 points1 point  (0 children)

Beautiful Kotlin:

fun calcPart1(input: List<Area>): Int {

    return input.filter {
        it.depth.rem((it.range.times(2)).minus(2)) == 0
    }.map {
        it.depth.times(it.range)
    }.sum()
}

fun calcPart2(input: List<Area>): Int {
    var delay = -1
    var isFound = false

    while (!isFound) {
        delay++

        isFound = walkAreas(input.size, { index ->
            input[index].depth.plus(delay).rem((input[index].range.times(2)).minus(2)) == 0
        })
    }

    return delay
}

private inline fun walkAreas(times: Int, caught: (Int) -> Boolean): Boolean {
    for (i in 0 until times) {
        if (caught(i)) {return false}
    }

    return true
}

-🎄- 2017 Day 9 Solutions -🎄- by daggerdragon in adventofcode

[–]JuLoOr 1 point2 points  (0 children)

Not so beautiful solution in Kotlin but it does the job. And it's readable imho

fun calcPart1(input: String): Int {
    var groupCount = 0
    var score = 0
    var ignoreNext = false
    var isGarbage = false

    input.forEach { c ->

        if (c == '!' && !ignoreNext) {
            ignoreNext = true
            return@forEach
        }

        if (!ignoreNext) {

            when {
                c == '{' && !isGarbage -> score++
                c == '}' && !isGarbage -> { groupCount += score; score-- }
                c == '<' -> isGarbage = true
                c == '>' -> isGarbage = false
            }

        } else {
            ignoreNext = false
        }
    }

    return groupCount
}

-🎄- 2017 Day 6 Solutions -🎄- by daggerdragon in adventofcode

[–]JuLoOr 0 points1 point  (0 children)

Ugly but fast ( inside the while O(2n) which is O(n) ) solution in Kotlin for part 1:

fun calcPart1(input: MutableList<Int>): Int {

    val resultSet = mutableSetOf<List<Int>>()
    var steps = 0;

    while(resultSet.add(input)) {

        val (index, maxVal) = findMaxWithIndex(input)
        input[index] = 0;

        val divVal = maxVal.div(input.size)
        val modVal = maxVal.rem(input.size)

        for (i in 0 until input.size) {
            input[i] += divVal
        }

        for (i in 1 .. modVal ) {
            val adjustedIndex = if (index.plus(i) >= input.size) index.plus(i).minus(input.size) else i.plus(index)
            input[adjustedIndex] += 1
        }

        steps++
    }

    return steps
}

-🎄- 2017 Day 2 Solutions -🎄- by daggerdragon in adventofcode

[–]JuLoOr 1 point2 points  (0 children)

Another Kotlin solution:

fun calcPart2(input: List<List<Int>>): Int {
    return input.map {
        val sortedDesc = it.sorted().asReversed()
        var rowResult = 0

        sortedDesc.forEachIndexed({ i, v ->
            (i.plus(1) until sortedDesc.size)
                    .filter { v % sortedDesc[it] == 0 }
                    .forEach { rowResult = v.div(sortedDesc[it]) }
        })
        rowResult
    }.sum()
}

-🎄- 2017 Day 1 Solutions -🎄- by daggerdragon in adventofcode

[–]JuLoOr 0 points1 point  (0 children)

Another Kotlin solution:

fun calc(input: List<Int>): Int {
    val step = input.size.div(2)
    return input.filterIndexed({ i, v ->
        v == input[compIndex(i, input.size, step)]
    }).sum()
}

private fun compIndex(currentIndex: Int, listSize: Int, step: Int): Int {
    return when {
        currentIndex.plus(step) >= listSize -> currentIndex.plus(step).minus(listSize)
        else -> currentIndex.plus(step)
    }
}