Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinJvmCompile

plugins {
kotlin("jvm")
id("com.github.johnrengelman.shadow")
id("com.gradleup.shadow")
id("org.jmailen.kotlinter")
id("com.adarshr.test-logger")
application
}

Expand Down Expand Up @@ -35,10 +36,6 @@ tasks.test {
maxHeapSize = "20g"
}

tasks.withType<KotlinJvmCompile> {
kotlinOptions.jvmTarget = "21"
}

tasks.withType<JavaExec> {
standardInput = System.`in`
}
Expand Down
Binary file removed kls_database.db
Binary file not shown.
11 changes: 11 additions & 0 deletions src/main/kotlin/cz/glubo/adventofcode/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ import cz.glubo.adventofcode.y2024.day15.y2024day15part1
import cz.glubo.adventofcode.y2024.day15.y2024day15part2
import cz.glubo.adventofcode.y2024.day16.y2024day16part1
import cz.glubo.adventofcode.y2024.day16.y2024day16part2
import cz.glubo.adventofcode.y2024.day17.y2024day17part1
import cz.glubo.adventofcode.y2024.day17.y2024day17part2
import cz.glubo.adventofcode.y2024.day2.y2024day2part1
import cz.glubo.adventofcode.y2024.day2.y2024day2part2
import cz.glubo.adventofcode.y2024.day3.y2024day3part1
Expand Down Expand Up @@ -137,6 +139,13 @@ class InputToLongCommand(
override suspend fun execute(input: Input) = action(input)
}

@Command(mixinStandardHelpOptions = true)
class InputToStringCommand(
private val action: suspend (input: Input) -> String,
) : InputCommand<String>() {
override suspend fun execute(input: Input) = action(input)
}

@Command(mixinStandardHelpOptions = true)
class LinesToIntCommand(
private val action: suspend (Flow<String>) -> Int,
Expand Down Expand Up @@ -252,6 +261,8 @@ fun main(args: Array<String>) {
"2024day15p2" to InputToLongCommand { y2024day15part2(it) },
"2024day16p1" to InputToLongCommand { y2024day16part1(it) },
"2024day16p2" to InputToLongCommand { y2024day16part2(it) },
"2024day17p1" to InputToStringCommand { y2024day17part1(it) },
"2024day17p2" to InputToLongCommand { y2024day17part2(it).toLong() },
)

val cmd = CommandLine(MyHelpCommand())
Expand Down
4 changes: 4 additions & 0 deletions src/main/kotlin/cz/glubo/adventofcode/utils/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -261,3 +261,7 @@ fun IntRange.split(boundary: Int): Pair<IntRange?, IntRange?> =
}

fun Int?.orMax() = this ?: Int.MAX_VALUE

fun <T> List<T>.isSame(other: List<T>) = (this.size == other.size) && this.indices.all {
this[it] == other[it]
}
3 changes: 2 additions & 1 deletion src/main/kotlin/cz/glubo/adventofcode/y2023/day4/Day4.kt
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,6 @@ fun pow2(n: Int): Int =
} else if (n == 0) {
1
} else {
(0..<n).fold(1) { acc: Int, _: Int -> acc * 2 }
// (0..<n).fold(1) { acc: Int, _: Int -> acc * 2 }
1.shl(n)
}
235 changes: 235 additions & 0 deletions src/main/kotlin/cz/glubo/adventofcode/y2024/day17/Day17.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
package cz.glubo.adventofcode.y2024.day17

import cz.glubo.adventofcode.utils.input.Input
import cz.glubo.adventofcode.utils.isSame
import cz.glubo.adventofcode.y2024.day17.ParameterType.COMBO
import cz.glubo.adventofcode.y2024.day17.ParameterType.LITERAL
import io.klogging.noCoLogger
import kotlinx.coroutines.flow.toList
import java.math.BigInteger

val logger = noCoLogger({}.javaClass.toString())

enum class ParameterType {
LITERAL,
COMBO,
}

enum class OPCODE(
val opcode: Int,
val type: ParameterType,
) {
ADV(0, COMBO),
BXL(1, LITERAL),
BST(2, COMBO),
JNZ(3, LITERAL),
BXC(4, LITERAL),
OUT(5, COMBO),
BDV(6, COMBO),
CDV(7, COMBO),
}

data class State(
var A: Long,
var B: Long,
var C: Long,
var pointer: Int,
var output: String,
)

typealias Program = List<Pair<OPCODE, Int>>

fun combo(
state: State,
param: Int,
): Long =
when (param) {
0, 1, 2, 3 -> param.toLong()
4 -> state.A
5 -> state.B
6 -> state.C
7 -> error("reserved")
else -> error("unknow combo value $param")
}

infix fun String.join(other: String) =
if (this.isNotBlank()) {
"$this,$other"
} else {
other
}

fun advance(
state: State,
program: Program,
): Boolean {
val op = program.getOrNull((state.pointer / 2).toInt())
logger.debug { "op $op state $state" }
return when (op?.first) {
OPCODE.ADV -> {
state.A /= pow2(combo(state, op.second))
state.pointer += 2
true
}

OPCODE.BXL -> {
state.B = state.B.xor(op.second.toLong())
state.pointer += 2
true
}

OPCODE.BST -> {
state.B = combo(state, op.second) % 8.toLong()
state.pointer += 2
true
}

OPCODE.JNZ -> {
if (state.A == 0L) {
state.pointer += 2
true
} else {
state.pointer = op.second
true
}
}

OPCODE.BXC -> {
state.B = state.B.xor(state.C)
state.pointer += 2
true
}

OPCODE.OUT -> {
state.output = state.output join combo(state, op.second).mod(8.toLong()).toString()
state.pointer += 2
true
}

OPCODE.BDV -> {
state.B = state.A / pow2(combo(state, op.second))
state.pointer += 2
true
}

OPCODE.CDV -> {
state.C = state.A / pow2(combo(state, op.second))
state.pointer += 2
true
}

null -> {
false
}
}
}

fun String.parseProgramRaw() =
this
.removePrefix("Program: ")
.split(",")
.map { it.toInt() }

fun List<Int>.toProgram() = this
.chunked(2)
.map { (o, p) ->
OPCODE.entries.first { it.opcode == o } to p
}

fun String.parseProgram() =
this.parseProgramRaw()
.toProgram()

suspend fun y2024day17part1(input: Input): String {
logger.info("year 2024 day 17 part 1")
val lines = input.lineFlow().toList()
val a = lines[0].removePrefix("Register A: ").toLong()
val b = lines[1].removePrefix("Register B: ").toLong()
val c = lines[2].removePrefix("Register C: ").toLong()
val program: Program =
lines[4].parseProgram()

val currentState =
State(
A = a,
B = b,
C = c,
pointer = 0,
output = "",
)
var i = 0
while (i < 100) {
val runNext = advance(currentState, program)
if (!runNext) {
return currentState.output
}
i++
}
return currentState.output
}

fun run(
program: Program,
a: Long,
): List<Int> {
val currentState =
State(
A = a,
B = 0L,
C = 0L,
pointer = 0,
output = "",
)
var i = 0
while (i < 1000) {
val runNext = advance(currentState, program)
if (!runNext) {
return currentState.output.split(",").map { it.toInt() }
}
i++
}
return currentState.output.split(",").map { it.toInt() }
}

fun pow2(n: Long): Long =
if (n < 0) {
throw IllegalArgumentException()
} else if (n == 0L) {
1L
} else {
1L.shl(n.toInt())
}

suspend fun y2024day17part2(input: Input): Long {
val lines = input.lineFlow().toList()
val wanted = lines[4].parseProgramRaw()
val program: Program = wanted.toProgram()

fun findMatch(wanted: List<Int>): List<Long> {
return when (wanted.size) {
0 -> listOf(0L)
else -> {
val smallerACandidates = findMatch(wanted.drop(1))

smallerACandidates.flatMap { smallerA ->
(0..7).mapNotNull { i ->
val a = smallerA * 8.toLong() + i.toLong()
val output = run(program, a)
logger.debug { "$smallerA, $a: $output, $wanted}" }
if (wanted.isSame(output)) {
logger.debug { "HIT" }
a
} else {
null
}
}
}
}
}

}

val suitableAList = findMatch(wanted)
logger.debug { suitableAList }
return suitableAList.minOrNull() ?: error("not found")
}
Loading
Loading