~macaptain/advent-of-code

356d2ac29abee3ecdee94c022234459ffb8620fd — Michael Captain 2 months ago 2f225ec
Complete 2019 Day 19
A src/main/advent2019/Day19.kt => src/main/advent2019/Day19.kt +31 -0
@@ 0,0 1,31 @@
package advent2019

import common.Point
import common.cartesianProduct

fun solve19a(program: String): Int {
    return (0..49).cartesianProduct(0..49).count { (x, y) ->
        val computer = runProgram(program, x, y)
        computer.output()!! == 1L
    }
}

fun solve19b(program: String): Int {
    // numbers chosen arbitrarily large enough to solve the problem...
    val tractorBeam = (0..1500).cartesianProduct(0..1500).associate { (x, y) ->
        val computer = runProgram(program, x, y)
        Point(x, y) to (computer.output()!! == 1L)
    }
    val nearestPoint = tractorBeam.keys.sortedBy { it.pythagoreanDistance() }.first {
        (it.x until it.x + 100).cartesianProduct(it.y until it.y + 100).all { (x2, y2) ->
            tractorBeam[Point(x2, y2)] ?: false
        }
    }
    return nearestPoint.x * 10_000 + nearestPoint.y
}

fun main() {
    val program = input("day19").first()
    println(solve19a(program)) // 181
    println(solve19b(program)) // 4240964
}
\ No newline at end of file

M src/main/common/Extensions.kt => src/main/common/Extensions.kt +4 -0
@@ 50,4 50,8 @@ fun <T> Iterable<T>.productOf(selector: (T) -> Long): Long {
        product *= selector(element)
    }
    return product
}

fun <S, T> Iterable<S>.cartesianProduct(o: Iterable<T>): Iterable<Pair<S, T>> {
    return flatMap { lhs -> o.map { rhs -> lhs to rhs } }
}
\ No newline at end of file

A src/main/resources/advent2019/day19_input.txt => src/main/resources/advent2019/day19_input.txt +1 -0
@@ 0,0 1,1 @@
109,424,203,1,21102,1,11,0,1105,1,282,21102,1,18,0,1105,1,259,2102,1,1,221,203,1,21101,31,0,0,1105,1,282,21101,38,0,0,1105,1,259,20102,1,23,2,21201,1,0,3,21102,1,1,1,21101,57,0,0,1106,0,303,2101,0,1,222,20101,0,221,3,21001,221,0,2,21101,259,0,1,21101,80,0,0,1106,0,225,21102,1,97,2,21102,1,91,0,1105,1,303,1201,1,0,223,20102,1,222,4,21101,259,0,3,21101,225,0,2,21102,1,225,1,21102,1,118,0,1106,0,225,21001,222,0,3,21102,1,21,2,21102,133,1,0,1106,0,303,21202,1,-1,1,22001,223,1,1,21101,0,148,0,1105,1,259,1201,1,0,223,20101,0,221,4,21001,222,0,3,21101,14,0,2,1001,132,-2,224,1002,224,2,224,1001,224,3,224,1002,132,-1,132,1,224,132,224,21001,224,1,1,21101,195,0,0,106,0,109,20207,1,223,2,20102,1,23,1,21102,-1,1,3,21101,0,214,0,1106,0,303,22101,1,1,1,204,1,99,0,0,0,0,109,5,2101,0,-4,249,21201,-3,0,1,21202,-2,1,2,21201,-1,0,3,21101,0,250,0,1106,0,225,22101,0,1,-4,109,-5,2105,1,0,109,3,22107,0,-2,-1,21202,-1,2,-1,21201,-1,-1,-1,22202,-1,-2,-2,109,-3,2105,1,0,109,3,21207,-2,0,-1,1206,-1,294,104,0,99,21202,-2,1,-2,109,-3,2105,1,0,109,5,22207,-3,-4,-1,1206,-1,346,22201,-4,-3,-4,21202,-3,-1,-1,22201,-4,-1,2,21202,2,-1,-1,22201,-4,-1,1,22102,1,-2,3,21101,0,343,0,1105,1,303,1105,1,415,22207,-2,-3,-1,1206,-1,387,22201,-3,-2,-3,21202,-2,-1,-1,22201,-3,-1,3,21202,3,-1,-1,22201,-3,-1,2,22101,0,-4,1,21102,1,384,0,1106,0,303,1105,1,415,21202,-4,-1,-4,22201,-4,-3,-4,22202,-3,-2,-2,22202,-2,-4,-4,22202,-3,-2,-3,21202,-4,-1,-2,22201,-3,-2,1,21202,1,1,-4,109,-5,2106,0,0