aaand - back to day1 - I believe I managed to do O...
# advent-of-code
j
aaand - back to day1 - I believe I managed to do O(n^2) in part2 and kind-of O(n) in part1 (kind-of, because it requires input to be sorted, so O(nlogn) effectively). testable version on https://jakubgwozdz.github.io/advent-of-code-2020/day01/ - I wonder if I missed any edge case
the inner loop looks like that:
Copy code
internal fun findIndex(
    sortedEntries: List<Int>,
    expectedSum: Int,
    indexOfExcluded: Int = -1
): Int {

    var startIndex = indexOfExcluded + 1
    var endIndex = sortedEntries.lastIndex

    while (startIndex < endIndex) {
        val sum = sortedEntries[startIndex] + sortedEntries[endIndex]
        comparisons++
        when {
            sum == expectedSum -> return startIndex
            sum < expectedSum -> startIndex++
            sum > expectedSum -> endIndex--
            else -> error("wtf")
        }
    }

    return -1
}
and searches the entries in one go
j
I like your UI, very cool!
🙏 1
1
j
tried with the idea last year, on https://jakubgwozdz.github.io/JSTry104/ and I thought this year to do all of them puzzles in the browser 🙂
👍 1
t
This UI is super cool!
b
What did you made that UI with?
n
You can do actual O(N) and O(N^2) by using Map instead of sorting, though it may actually be slower for this number of entries, given how optimized sorting typically is
j
@bjonnh Kotlin all the way. kotlinx.html (I was so preoccupied with whether or not I could, I didn't stop to think if I should). also, css is from bulma. I'm backend dev, I don't know what bulma is, I just googled it, and that reduced my time spent on centering checkboxes by half (last year I tried bootstrap, but was not happy with results)
😄 1
b
really cool