Skip to content

Commit

Permalink
a little 2019
Browse files Browse the repository at this point in the history
  • Loading branch information
elwaxoro committed Dec 31, 2024
1 parent 0b5215c commit ffdd189
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 9 deletions.
6 changes: 4 additions & 2 deletions advent/src/test/kotlin/org/elwaxoro/advent/y2019/Dec11.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.elwaxoro.advent.y2019

import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
Expand All @@ -26,6 +27,7 @@ class Dec11: PuzzleDayTester(11, 2019) {
*/
override fun part2(): Any = goRobot(Coord(0, 0, '1')).filter { it.d == '1' }.map { it.copyD('X') }.printify(invert = true, empty = ' ')

@OptIn(DelicateCoroutinesApi::class)
private fun goRobot(start: Coord) = runBlocking {
ElfCode(loadToLong(delimiter = ",")).let { robot ->
var pos = start
Expand All @@ -35,9 +37,9 @@ class Dec11: PuzzleDayTester(11, 2019) {
val output = Channel<Long>(capacity = Channel.UNLIMITED)
input.send(pos.dLong())
launch {
robot.runner({it.addAll(listOf(0L).padTo(2000))}, { input.receive() }, { output.send(it) })
robot.runner({it.addAll(listOf(0L).padTo(2000))}, { input.receive() }, { output.send(it) }, { output.close() })
}
while (robot.isRunning) {
while (!output.isClosedForReceive) {
// robot starts out every time already on the position it's going to paint, so record the output color here as a new coord
val out = output.receive()
visited.add(pos.copyD(out.toInt().digitToChar()))
Expand Down
90 changes: 90 additions & 0 deletions advent/src/test/kotlin/org/elwaxoro/advent/y2019/Dec13.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package org.elwaxoro.advent.y2019

import kotlinx.coroutines.runBlocking
import org.elwaxoro.advent.Coord
import org.elwaxoro.advent.PuzzleDayTester
import org.elwaxoro.advent.printify

/**
* Day 13: Care Package
*/
class Dec13 : PuzzleDayTester(13, 2019) {

override fun part1(): Any = runBlocking { Game(loadToLong(delimiter = ",")).play().coords.values.count { it == 'X' } } == 260

override fun part2(): Any = runBlocking { Game(loadToLong(delimiter = ","), play = true).play().score } == 12952

private class Game(
val code: List<Long>,
val coords: MutableMap<Coord, Char> = mutableMapOf(),
var paddle: Coord = Coord(-1, 0),
var ball: Coord = Coord(-1, 0),
var score: Int = 0,
val xyz: MutableList<Int> = mutableListOf(),
val play: Boolean = false,
) {

private fun Int.toPixel(): Char =
when (this) {
0 -> ' ' // empty
1 -> '#' // wall
2 -> 'X' // block
3 -> '-' // paddle
4 -> 'o' // ball
else -> throw IllegalStateException("Unknown tile id $this")
}

fun print() {
if (coords.isNotEmpty()) {
println(coords.map { it.key.copyD(it.value) }.printify())
} else {
println("NO DATA")
}
println("Score: $score")
}

private val joystick: () -> Long = {
if (paddle.x == ball.x) {
0
} else if (paddle.x < ball.x) {
1
} else {
-1
}
}

private val reader: (i: Long) -> Unit = {
xyz.add(it.toInt())
if (xyz.size == 3) {
if (xyz[0] < 0) {
score = xyz[2]
} else {
val c = Coord(xyz[0], xyz[1])
val d = xyz[2].toPixel()
coords[c] = d
if (d == '-') {
paddle = c
} else if (d == 'o') {
ball = c
}
}
xyz.clear()
}
}

suspend fun play(): Game {
ElfCode(code).runner(
setup = {
ElfCode.memExpander(100000).invoke(it)
if (play)
it[0] = 2
},
input = joystick,
output = reader,
exit = {
print()
})
return this
}
}
}
14 changes: 7 additions & 7 deletions advent/src/test/kotlin/org/elwaxoro/advent/y2019/ElfCode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import org.elwaxoro.advent.takeSplit
* Dec 5: added input, output, jump-if-true, jump-if-false, less-than, equals, position and immediate get modes, converted from Int to Long
* Dec 7: added async support (suspend functions for input / output to allow channels)
* Dec 9: added relative mode, expanding memory (use setup function to expand manually)
* Dec 11: added optional exit support, in case input/output are channels
*/
class ElfCode(
private val originalProgram: List<Long>,
var isRunning: Boolean = true,
private var relativeBase: Long = 0,
private var modes: String = "000",
private var prog: MutableList<Long> = mutableListOf(),
Expand All @@ -23,7 +23,6 @@ class ElfCode(
}

private fun reset() {
isRunning = true
prog = originalProgram.toMutableList()
modes = "000"
idx = 0L
Expand All @@ -32,15 +31,16 @@ class ElfCode(
suspend fun runner(
setup: (program: MutableList<Long>) -> Unit = { },
input: suspend () -> Long = { 1 },
output: suspend (out: Long) -> Unit = { println("Output: $it") }
output: suspend (out: Long) -> Unit = { println("Output: $it") },
exit: suspend () -> Unit = { },
): List<Long> {
reset()
setup.invoke(prog)

var isRunning = true
while (isRunning) {
val (m, op) = "${prog[idx.toInt()]}".padStart(5, padChar = '0').takeSplit(3)
modes = m
//println("modified $idx = ${prog[idx.toInt()]} into op $op with modes $modes $prog")
// println("modified $idx = ${prog[idx.toInt()]} into op $op with modes $modes $prog")
when (op.toInt()) {
1 -> { // addition
put(3, get(1) + get(2))
Expand Down Expand Up @@ -108,15 +108,15 @@ class ElfCode(
else -> throw IllegalStateException("Unknown opcode $op with mode $modes at idx $idx! Full prog: $prog")
}
}
exit.invoke()
return prog
}

private fun get(param: Int): Long = prog[mode(modes, param, idx + param)]
private fun put(param: Int, value: Long): Unit {
private fun put(param: Int, value: Long) {
prog[mode(modes, param, idx + param)] = value
}

// removed -1
private fun mode(modes: String, modeIdx: Int, codeIdx: Long): Int = mode(modes[modes.length - modeIdx].digitToInt(), codeIdx)

private fun mode(mode: Int, idx: Long): Int =
Expand Down
1 change: 1 addition & 0 deletions advent/src/test/resources/2019/Dec13.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1,380,379,385,1008,2249,380030,381,1005,381,12,99,109,2250,1102,1,0,383,1101,0,0,382,20101,0,382,1,20101,0,383,2,21102,1,37,0,1106,0,578,4,382,4,383,204,1,1001,382,1,382,1007,382,35,381,1005,381,22,1001,383,1,383,1007,383,23,381,1005,381,18,1006,385,69,99,104,-1,104,0,4,386,3,384,1007,384,0,381,1005,381,94,107,0,384,381,1005,381,108,1105,1,161,107,1,392,381,1006,381,161,1102,1,-1,384,1105,1,119,1007,392,33,381,1006,381,161,1102,1,1,384,20101,0,392,1,21102,21,1,2,21102,0,1,3,21101,0,138,0,1105,1,549,1,392,384,392,21002,392,1,1,21102,21,1,2,21101,3,0,3,21102,1,161,0,1105,1,549,1101,0,0,384,20001,388,390,1,20101,0,389,2,21102,180,1,0,1105,1,578,1206,1,213,1208,1,2,381,1006,381,205,20001,388,390,1,21002,389,1,2,21102,1,205,0,1105,1,393,1002,390,-1,390,1102,1,1,384,21001,388,0,1,20001,389,391,2,21101,0,228,0,1105,1,578,1206,1,261,1208,1,2,381,1006,381,253,21001,388,0,1,20001,389,391,2,21101,0,253,0,1106,0,393,1002,391,-1,391,1101,0,1,384,1005,384,161,20001,388,390,1,20001,389,391,2,21101,0,279,0,1106,0,578,1206,1,316,1208,1,2,381,1006,381,304,20001,388,390,1,20001,389,391,2,21102,304,1,0,1105,1,393,1002,390,-1,390,1002,391,-1,391,1101,0,1,384,1005,384,161,20101,0,388,1,21001,389,0,2,21102,1,0,3,21102,1,338,0,1106,0,549,1,388,390,388,1,389,391,389,20101,0,388,1,21001,389,0,2,21102,1,4,3,21101,365,0,0,1106,0,549,1007,389,22,381,1005,381,75,104,-1,104,0,104,0,99,0,1,0,0,0,0,0,0,260,15,18,1,1,17,109,3,21202,-2,1,1,21202,-1,1,2,21101,0,0,3,21102,414,1,0,1106,0,549,21202,-2,1,1,22101,0,-1,2,21102,1,429,0,1106,0,601,1201,1,0,435,1,386,0,386,104,-1,104,0,4,386,1001,387,-1,387,1005,387,451,99,109,-3,2105,1,0,109,8,22202,-7,-6,-3,22201,-3,-5,-3,21202,-4,64,-2,2207,-3,-2,381,1005,381,492,21202,-2,-1,-1,22201,-3,-1,-3,2207,-3,-2,381,1006,381,481,21202,-4,8,-2,2207,-3,-2,381,1005,381,518,21202,-2,-1,-1,22201,-3,-1,-3,2207,-3,-2,381,1006,381,507,2207,-3,-4,381,1005,381,540,21202,-4,-1,-1,22201,-3,-1,-3,2207,-3,-4,381,1006,381,529,21202,-3,1,-7,109,-8,2105,1,0,109,4,1202,-2,35,566,201,-3,566,566,101,639,566,566,2101,0,-1,0,204,-3,204,-2,204,-1,109,-4,2105,1,0,109,3,1202,-1,35,593,201,-2,593,593,101,639,593,593,21001,0,0,-2,109,-3,2105,1,0,109,3,22102,23,-2,1,22201,1,-1,1,21101,0,409,2,21102,1,437,3,21102,1,805,4,21102,1,630,0,1106,0,456,21201,1,1444,-2,109,-3,2106,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,2,0,2,0,0,0,2,2,0,0,2,0,2,2,2,0,2,2,0,0,2,2,0,0,2,2,2,2,2,2,2,0,1,1,0,0,2,2,0,0,0,2,2,2,0,0,0,2,2,2,0,2,0,2,0,2,0,2,0,2,0,2,2,2,2,2,0,1,1,0,0,2,2,2,2,0,2,0,2,2,2,2,2,0,0,0,2,0,2,2,0,2,0,0,2,2,0,2,2,0,0,0,1,1,0,2,0,2,2,2,2,0,0,0,2,2,0,0,2,2,2,0,2,2,2,2,0,2,2,0,0,2,0,2,0,0,0,1,1,0,2,0,2,2,0,2,2,0,0,0,0,2,0,2,2,2,0,2,0,0,2,2,2,0,0,0,0,2,2,2,0,0,1,1,0,2,0,0,0,0,0,2,0,2,0,2,0,2,0,0,2,2,2,0,0,0,2,0,0,0,0,0,2,2,2,2,0,1,1,0,2,2,2,2,0,0,0,0,2,2,2,0,2,2,0,0,2,2,0,2,2,0,2,0,2,2,0,2,2,0,2,0,1,1,0,2,0,0,2,2,0,2,2,2,0,2,2,0,0,2,0,0,0,0,2,2,0,2,2,2,2,2,0,2,2,0,0,1,1,0,0,0,0,2,2,0,0,2,2,0,0,0,2,2,2,2,0,2,0,2,2,2,0,2,0,0,0,0,0,0,2,0,1,1,0,0,2,2,0,0,0,2,0,2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,38,62,62,83,33,70,73,46,14,89,23,98,14,28,75,79,15,3,98,79,3,4,28,22,5,73,63,60,66,45,36,96,80,48,23,97,98,79,79,82,45,72,6,68,17,51,13,34,27,95,84,59,41,40,40,13,86,21,92,37,30,54,59,48,94,63,37,53,2,62,87,47,42,28,60,48,97,61,68,59,39,31,22,88,34,72,54,11,89,34,68,35,71,5,68,97,37,43,41,80,42,39,91,94,41,56,18,10,76,69,39,4,4,11,14,32,45,85,65,57,51,72,70,53,71,29,9,78,24,31,16,9,63,40,10,26,73,69,45,72,15,98,83,59,3,21,72,97,19,74,69,61,61,62,55,91,28,4,25,27,61,89,91,16,56,11,63,93,25,88,17,12,44,69,92,90,41,6,30,3,89,1,17,21,56,93,2,47,14,14,92,21,52,83,36,36,11,97,6,57,53,97,88,48,70,53,94,84,79,56,2,35,36,68,18,97,60,75,85,30,4,89,14,45,13,88,41,16,59,52,8,47,50,76,93,36,87,22,65,36,32,56,63,31,97,51,70,4,49,37,5,27,48,16,48,79,92,55,3,94,35,46,79,4,92,46,22,87,21,88,50,36,82,67,40,63,97,69,91,63,98,68,2,17,3,87,59,71,87,18,30,13,86,87,84,39,14,63,49,83,57,5,66,11,61,81,9,81,52,62,47,32,86,28,96,4,57,4,57,95,91,71,91,57,1,16,46,40,38,62,7,85,87,76,22,43,23,77,85,73,37,37,90,53,7,25,30,57,98,73,66,56,48,19,74,53,4,65,38,94,9,22,55,67,89,81,96,36,42,3,17,73,28,56,40,42,72,28,20,4,49,2,14,18,10,34,78,13,13,65,6,55,47,97,37,24,51,88,42,22,60,35,2,10,27,37,13,51,53,24,26,81,62,68,30,25,34,9,29,51,6,22,76,21,40,38,97,7,64,31,80,64,10,89,69,50,64,74,94,22,75,30,41,48,58,77,70,48,22,86,10,35,82,84,8,23,28,21,79,98,43,34,19,71,39,80,35,37,81,33,8,35,56,68,23,2,38,32,32,86,60,37,42,53,10,16,5,45,92,20,78,90,25,19,94,44,7,81,22,3,4,37,14,26,3,42,92,22,44,58,28,63,41,81,94,85,2,96,63,67,87,42,55,27,22,94,14,86,19,88,65,93,91,11,47,67,98,28,6,43,46,41,33,27,84,96,39,40,54,81,39,68,85,79,48,59,27,68,34,51,36,64,8,54,44,17,58,54,83,17,56,79,57,5,52,25,8,73,23,63,89,91,72,74,4,12,97,67,6,67,88,52,92,97,28,75,85,64,29,20,5,35,7,54,38,14,93,62,59,74,93,86,91,82,23,83,1,35,5,21,18,71,7,39,8,32,68,57,95,67,39,19,98,89,17,87,37,78,54,36,22,30,35,68,95,61,31,72,86,85,33,12,81,91,1,23,63,91,34,5,86,70,65,69,72,20,84,38,13,94,47,22,40,85,15,18,95,26,68,63,59,38,73,24,69,31,21,87,90,66,87,84,30,79,76,55,33,55,33,94,7,55,380030

0 comments on commit ffdd189

Please sign in to comment.