:grey_exclamation:Day 11 Solution Thread:heavy_hea...
# advent-of-code
b
Day 11 Solution Thread❣️
d
Ah, I knew there would be a Game of Life in there on one of the days!
Darn off-by-one errors crushed me on this
these are starting to get too unwieldy to copy-paste into slack
a
I reused a Grid2D class (created for AoC 2018) which wraps <Array<Array<Char>>> and provides Direction & Coord classes. I had to add some extra methods Grid2D.firstSeats() & Grid2D.firstSeatInDirection() shown in the snippet to solve Part 2.
yes black 1
n
debating whether to go back and refactor this to make it "pretty". I did most stuff in the original solution with pretty raw for loops and integers (well, the hard part at least)
I tried refactoring a bit, Kotlin seems to complain here because the type after the
map
is Sequence<Any> rather than Sequence<Int>. Can anyone point out the easiest way to solve this? Is my return qualified correctly?
Copy code
fun SeatMap.visibleNeighbors(p: Point) = directionPoints().map {
    var point = p
    while (true) {
        point += it
        if (point !in this || getSeat(point) == Seat.EMPTY) {
            return@map 0
        }
        if (getSeat(point) == Seat.OCCUPIED) {
            return@map 1
        }
    }
}.sum()
hmm, if I add a 0 on the last line of the map it compiles 😞 Any less awkward solution?
In the end I ended up refactoring to this more functional approach:
Copy code
fun SeatMap.visibleNeighbors(p: Point) = directionPoints().map { dirPoint ->

    val first = generateSequence(p + dirPoint) { it + dirPoint }
        .filter { getSeat(it) != Seat.EMPTY }
        .first()

    when (getSeat(first)) {
        null, Seat.EMPTY -> 0
        Seat.OCCUPIED -> 1
        else -> throw Exception("")
    }
}.sum()
e
I took today off work because I'm using up vacation time that goes away at the end of the year, so I took my time with this one. I'm pretty happy with how that turned out.
metal 1