Post

AoC - "Not Quite Lisp"

My solution to Advent of Code 2015 Day 1

See the challenge

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package adventOfCode.y2015.d01

import adventOfCode.lib.Solver

data class Floor(val level: Int, val idx: Int)

class Solution : Solver {
    override fun part1(input: String): Int {
        return floors(input).last().level
    }

    override fun part2(input: String): Int {
        return floors(input).find { floor -> floor.level == -1 }!!.idx + 1
    }

    private fun floors(input: String) = sequence {
        var level = 0

        input.forEachIndexed { idx, char ->
            level += if (char == '(') 1 else -1
            yield(Floor(level, idx))
        }
    }
}
This post is licensed under CC BY 4.0 by the author.