diff --git a/src/main/kotlin/Day14.kt b/src/main/kotlin/Day14.kt new file mode 100644 index 0000000..9d57f89 --- /dev/null +++ b/src/main/kotlin/Day14.kt @@ -0,0 +1,94 @@ +import kotlin.system.exitProcess +import utils.AoCDay +import utils.Vec2D +import utils.splitAndMapToInt + +class Day14 : AoCDay() { + val robots = input.map { + val (loc, dir) = it.split(" ").map { + it.drop(2).splitAndMapToInt(",") + } + Vec2D(loc.first(), loc.last()) to Vec2D(dir.first(), dir.last()) + } + val spaceWidth = 101 + val spaceHeight = 103 + + override fun part1(): Any { + val locations = simulate(100) + val middleX = spaceWidth / 2 + val middleY = spaceHeight / 2 + val TL = locations.filter { it.x in 0.. { + var space = robots.toMutableList() + repeat(times) { + val newSpace = mutableListOf>() + for ((loc, dir) in space) { + var newLoc = loc + dir + while (newLoc.y < 0) { + newLoc += Vec2D(0, spaceHeight) + } + while (newLoc.y >= spaceHeight) { + newLoc -= Vec2D(0, spaceHeight) + } + while (newLoc.x < 0) { + newLoc += Vec2D(spaceWidth, 0) + } + while (newLoc.x >= spaceWidth) { + newLoc -= Vec2D(spaceWidth, 0) + } + //println("$loc $dir -> $newLoc") + newSpace += newLoc to dir + } + space = newSpace + printSpace(space.map { it.first }, true, it + 1) + } + return space.map { it.first } + } + + fun printSpace(robots: List, lookForTree: Boolean, iteration: Int) { + var potentialTree = false + for (y in 0 until spaceHeight) { + var row = "" + for (x in 0 until spaceWidth) { + val count = robots.count { it == Vec2D(x, y) } + row += if (count == 0) { + "." + } else { + "$count" + } + } + if (row.contains("111111111") && lookForTree) { + potentialTree = true + } + if (!lookForTree) { + println(row) + } + } + if (lookForTree && potentialTree) { + printSpace(robots, false, iteration) + println() + println("Is this a tree? (y/n)") + val input = readln() + if (input == "y") { + println("Part B: $iteration") + exitProcess(0) + } + } + } +} + +fun main() { + Day14().execute() +}