Created
December 16, 2022 11:01
-
-
Save saidaspen/0756849c11edad6c2fc9f49ad1a49e68 to your computer and use it in GitHub Desktop.
AOC 2022 Day 16
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import se.saidaspen.aoc.util.* | |
fun main() = Dayxx.run() | |
object Dayxx : Day(2022, 16) { | |
private val valves = input.lines().associate { it.split(" ")[1] to ints(it)[0] } | |
private val relevantValves = valves.entries.filter { it.value > 0 }.map { it.key to it.value }.toMap() | |
private val connections = input.lines().map { it.split(" ") }.associate { | |
it[1] to it.drop(9).map { c -> c.replace(",", "").trim() }.toList() | |
} | |
private val pairs = combinations(valves.keys.toTypedArray(), 2).toList().map { P(it[0], it[1]) } | |
private var distances = | |
pairs.associateWith { p -> bfs(p.first, { it == p.second }, { connections[it]!! }).second }.toMap() | |
override fun part1(): Any { | |
class State(val pos: String, val turnedOn: List<String>, val score: Int = 0, val time: Int = 30) | |
val queue = mutableListOf<State>().toArrayDeque() | |
val start = State("AA", mutableListOf(), 0, 30) | |
queue.addLast(start) | |
var best = start | |
while (queue.isNotEmpty()) { | |
val curr = queue.removeFirst() | |
best = if (curr.score > best.score) curr else best | |
if (curr.time < 1) continue | |
relevantValves.entries.filter { !curr.turnedOn.contains(it.key) }.map { | |
val nextOn = curr.turnedOn.toMutableList() | |
nextOn.add(it.key) | |
val dist = distance(curr.pos, it.key) | |
val nextTimeLeft = curr.time - dist - 1 | |
State(it.key, nextOn, score = curr.score + nextTimeLeft * it.value, time = nextTimeLeft) | |
}.forEach { queue.addLast(it) } | |
} | |
return best.score | |
} | |
private fun distance(from: String, to: String) : Int { | |
return if (distances.containsKey(P(from, to))) distances[P(from, to)]!! else distances[P(to, from)]!! | |
} | |
override fun part2(): Any { | |
// W T F ? | |
return "" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
LOL, I know right