diff --git a/src/main/kotlin/com/basdado/adventofcode/day11/Day11.kt b/src/main/kotlin/com/basdado/adventofcode/day11/Day11.kt new file mode 100644 index 0000000..98e5528 --- /dev/null +++ b/src/main/kotlin/com/basdado/adventofcode/day11/Day11.kt @@ -0,0 +1,119 @@ +package com.basdado.adventofcode.day11 + +import com.basdado.adventofcode.IntCodeProgram +import com.basdado.adventofcode.loadProgramData + +const val DAY11_INPUT_PATH = "/day/11/input.txt" + +fun main() { + Day11.puzzle1() + Day11.puzzle2() +} + +object Day11 { + + fun puzzle1() { + val programData = loadProgramData(DAY11_INPUT_PATH) + val program = IntCodeProgram(programData + LongArray(16384), longArrayOf()) + val hull = mutableMapOf() + paintHull(hull, program) + printHull(hull) + println(hull.size) + } + + fun puzzle2() { + + val programData = loadProgramData(DAY11_INPUT_PATH) + val program = IntCodeProgram(programData + LongArray(16384), longArrayOf()) + val hull = mutableMapOf( + // Paint position 0,0 white + Pair(Vector2i(0, 0), 1) + ) + paintHull(hull, program) + printHull(hull) + } + + private fun printHull(hull: MutableMap) { + val min = Vector2i( + hull.keys.map { it.x }.min()!!, + hull.keys.map { it.y }.min()!! + ) + val max = Vector2i( + hull.keys.map { it.x }.max()!!, + hull.keys.map { it.y }.max()!! + ) + (max.y downTo min.y).forEach { y -> + (min.x..max.x).forEach { x -> + print( + when (hull[Vector2i(x, y)]) { + 0 -> "." + 1 -> "#" + null -> " " + else -> "E" + } + ) + } + println() + } + } + + private fun paintHull( + hull: MutableMap, + program: IntCodeProgram + ) { + var pos = Vector2i(0, 0) + var dir = UP + + var lastHullPaintingSize = 0 + var hullPaintingSizeConstant = 0 + while (hullPaintingSizeConstant < 1000) { + lastHullPaintingSize = hull.size + + program.pushInput((hull[pos] ?: 0).toLong()) + val color = program.executeUntilOutput() ?: break + hull[pos] = color.toInt() + + val rotate = program.executeUntilOutput() ?: break + val newDir = if (rotate == 0L) { + // Turn left + when (dir) { + UP -> LEFT + LEFT -> DOWN + DOWN -> RIGHT + RIGHT -> UP + else -> throw IllegalStateException("Invalid direction: $dir") + } + } else { + when (dir) { + UP -> RIGHT + RIGHT -> DOWN + DOWN -> LEFT + LEFT -> UP + else -> throw IllegalStateException("Invalid direction: $dir") + } + } + pos += newDir + dir = newDir + + if (lastHullPaintingSize == hull.size) { + hullPaintingSizeConstant++ + } else { + hullPaintingSizeConstant = 0 + } + } + } + + data class Vector2i(val x: Int, val y: Int) { + + operator fun div(d: Int) = Vector2i(x / d, y / d) + operator fun minus(n: Int) = Vector2i(x - n, y - n) + operator fun minus(v: Vector2i) = Vector2i(x - v.x, y - v.y) + operator fun plus(n: Int) = Vector2i(x + n, y + n) + operator fun plus(v: Vector2i) = Vector2i(x + v.x, y + v.y) + } + + val LEFT = Vector2i(-1, 0) + val RIGHT = Vector2i(1, 0) + val UP = Vector2i(0, 1) + val DOWN = Vector2i(0, -1) +} \ No newline at end of file diff --git a/src/main/resources/day/11/input.txt b/src/main/resources/day/11/input.txt new file mode 100644 index 0000000..49c351e --- /dev/null +++ b/src/main/resources/day/11/input.txt @@ -0,0 +1 @@ +3,8,1005,8,310,1106,0,11,0,0,0,104,1,104,0,3,8,1002,8,-1,10,101,1,10,10,4,10,1008,8,0,10,4,10,1001,8,0,29,1,2,11,10,1,1101,2,10,2,1008,18,10,2,106,3,10,3,8,1002,8,-1,10,1001,10,1,10,4,10,1008,8,1,10,4,10,102,1,8,67,2,105,15,10,3,8,1002,8,-1,10,101,1,10,10,4,10,1008,8,0,10,4,10,1001,8,0,93,2,1001,16,10,3,8,102,-1,8,10,1001,10,1,10,4,10,1008,8,1,10,4,10,102,1,8,119,3,8,1002,8,-1,10,1001,10,1,10,4,10,1008,8,1,10,4,10,101,0,8,141,2,7,17,10,1,1103,16,10,3,8,1002,8,-1,10,101,1,10,10,4,10,108,0,8,10,4,10,102,1,8,170,3,8,1002,8,-1,10,1001,10,1,10,4,10,1008,8,1,10,4,10,1002,8,1,193,1,7,15,10,2,105,13,10,1006,0,92,1006,0,99,3,8,1002,8,-1,10,101,1,10,10,4,10,108,1,8,10,4,10,101,0,8,228,1,3,11,10,1006,0,14,1006,0,71,3,8,1002,8,-1,10,101,1,10,10,4,10,1008,8,0,10,4,10,101,0,8,261,2,2,2,10,1006,0,4,3,8,102,-1,8,10,101,1,10,10,4,10,108,0,8,10,4,10,101,0,8,289,101,1,9,9,1007,9,1049,10,1005,10,15,99,109,632,104,0,104,1,21101,0,387240009756,1,21101,327,0,0,1105,1,431,21101,0,387239486208,1,21102,1,338,0,1106,0,431,3,10,104,0,104,1,3,10,104,0,104,0,3,10,104,0,104,1,3,10,104,0,104,1,3,10,104,0,104,0,3,10,104,0,104,1,21102,3224472579,1,1,21101,0,385,0,1106,0,431,21101,0,206253952003,1,21102,396,1,0,1105,1,431,3,10,104,0,104,0,3,10,104,0,104,0,21102,709052072296,1,1,21102,419,1,0,1105,1,431,21102,1,709051962212,1,21102,430,1,0,1106,0,431,99,109,2,21202,-1,1,1,21102,1,40,2,21102,462,1,3,21102,452,1,0,1105,1,495,109,-2,2105,1,0,0,1,0,0,1,109,2,3,10,204,-1,1001,457,458,473,4,0,1001,457,1,457,108,4,457,10,1006,10,489,1101,0,0,457,109,-2,2105,1,0,0,109,4,2102,1,-1,494,1207,-3,0,10,1006,10,512,21101,0,0,-3,22101,0,-3,1,21202,-2,1,2,21102,1,1,3,21101,531,0,0,1105,1,536,109,-4,2106,0,0,109,5,1207,-3,1,10,1006,10,559,2207,-4,-2,10,1006,10,559,21202,-4,1,-4,1105,1,627,22102,1,-4,1,21201,-3,-1,2,21202,-2,2,3,21102,1,578,0,1105,1,536,21202,1,1,-4,21102,1,1,-1,2207,-4,-2,10,1006,10,597,21101,0,0,-1,22202,-2,-1,-2,2107,0,-3,10,1006,10,619,21201,-1,0,1,21102,1,619,0,106,0,494,21202,-2,-1,-2,22201,-4,-2,-4,109,-5,2106,0,0