this post was submitted on 05 Dec 2024
26 points (100.0% liked)

Advent Of Code

982 readers
90 users here now

An unofficial home for the advent of code community on programming.dev!

Advent of Code is an annual Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.

AoC 2024

Solution Threads

M T W T F S S
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25

Rules/Guidelines

Relevant Communities

Relevant Links

Credits

Icon base by Lorc under CC BY 3.0 with modifications to add a gradient

console.log('Hello World')

founded 1 year ago
MODERATORS
 

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

you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 1 points 3 weeks ago

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()