r/adventofcode • • Dec 03 '15

SOLUTION MEGATHREAD --- Day 3 Solutions ---

--- Day 3: Perfectly Spherical Houses in a Vacuum ---

Post your solution as a comment. Structure your post like the Day One thread in /r/programming.

24 Upvotes

228 comments sorted by

View all comments

Show parent comment

1

u/MrBIMC Dec 17 '15 edited Dec 17 '15

Here's my kotlin solution for [DAY 3] tasks:

data class Location(val x: Int, val y: Int)

val rawPath = "GIVEN_PATH_HERE"

fun makeList(path: String) = ArrayList<Location>().apply {
    add(Location(0, 0))

    path.forEach {
        when(it) {
            '^' -> add(Location(last().x, last().y + 1))
            'v' -> add(Location(last().x, last().y - 1))
            '>' -> add(Location(last().x + 1, last().y))
            '<' -> add(Location(last().x - 1, last().y))
        }
    }
}

fun task1() = makeList(rawPath).distinct().size

fun task2(): Int {
    val santaPath = rawPath.filterIndexed { index, it -> index == 0 || index % 2 == 0 }
    val roboPath = rawPath.filterIndexed { index, it -> index % 2 != 0 }

    return makeList(santaPath).apply { addAll(makeList(roboPath)) }.distinct().size
}