Day 5: Print Queue
Megathread guidelines
- Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
- You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL
FAQ
- What is this?: Here is a post with a large amount of details: https://programming.dev/post/6637268
- Where do I participate?: https://adventofcode.com/
- Is there a leaderboard for the community?: We have a programming.dev leaderboard with the info on how to join in this post: https://programming.dev/post/6631465
Kotlin
That was an easy one, once you define a comparator function. (At least when you have a sorting function in your standard-library.) The biggest part was the parsing. lol
import kotlin.text.Regex fun main() { fun part1(input: List<String>): Int = parseInput(input).sumOf { if (it.isCorrectlyOrdered()) it[it.size / 2].pageNumber else 0 } fun part2(input: List<String>): Int = parseInput(input).sumOf { if (!it.isCorrectlyOrdered()) it.sorted()[it.size / 2].pageNumber else 0 } val testInput = readInput("Day05_test") check(part1(testInput) == 143) check(part2(testInput) == 123) val input = readInput("Day05") part1(input).println() part2(input).println() } fun parseInput(input: List<String>): List<List<Page>> { val (orderRulesStrings, pageSequencesStrings) = input.filter { it.isNotEmpty() }.partition { Regex("""\d+\|\d+""").matches(it) } val orderRules = orderRulesStrings.map { with(it.split('|')) { this[0].toInt() to this[1].toInt() } } val orderRulesX = orderRules.map { it.first }.toSet() val pages = orderRulesX.map { pageNumber -> val orderClasses = orderRules.filter { it.first == pageNumber }.map { it.second } Page(pageNumber, orderClasses) }.associateBy { it.pageNumber } val pageSequences = pageSequencesStrings.map { sequenceString -> sequenceString.split(',').map { pages[it.toInt()] ?: Page(it.toInt(), emptyList()) } } return pageSequences } /* * An order class is an equivalence class for every page with the same page to be printed before. */ data class Page(val pageNumber: Int, val orderClasses: List<Int>): Comparable<Page> { override fun compareTo(other: Page): Int = if (other.pageNumber in orderClasses) -1 else if (pageNumber in other.orderClasses) 1 else 0 } fun List<Page>.isCorrectlyOrdered(): Boolean = this == this.sorted()