~macaptain/advent-of-code

037666e5f3fed35ae6506e1a650705e99e6306bf — Michael Captain 1 year, 7 months ago 28115c0
Update to Kotlin 1.7 and JVM 17
M .idea/kotlinc.xml => .idea/kotlinc.xml +2 -2
@@ 1,6 1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
  <component name="Kotlin2JsCompilerArguments">
    <option name="sourceMapEmbedSources" />
  <component name="Kotlin2JvmCompilerArguments">
    <option name="jvmTarget" value="17" />
  </component>
</project>
\ No newline at end of file

M .idea/misc.xml => .idea/misc.xml +5 -1
@@ 11,7 11,11 @@
      </list>
    </option>
  </component>
  <component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="false" project-jdk-name="corretto-11" project-jdk-type="JavaSDK">
  <component name="PWA">
    <option name="enabled" value="true" />
    <option name="wasEnabledAtLeastOnce" value="true" />
  </component>
  <component name="ProjectRootManager" version="2" languageLevel="JDK_17" project-jdk-name="corretto-17" project-jdk-type="JavaSDK">
    <output url="file://$PROJECT_DIR$/out" />
  </component>
</project>
\ No newline at end of file

M pom.xml => pom.xml +1 -1
@@ 12,7 12,7 @@

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <kotlin.version>1.6.10</kotlin.version>
        <kotlin.version>1.7.10</kotlin.version>
        <kotlin.code.style>official</kotlin.code.style>
        <junit.version>5.8.2</junit.version>
    </properties>

M src/main/advent2017/Day08.kt => src/main/advent2017/Day08.kt +1 -1
@@ 34,7 34,7 @@ fun solve8a(lines: List<String>): Int {
        val (result, condition) = it.split(" if ")
        if (evalCondition(condition)) doResult(result)
    }
    return vars.values.maxOrNull()!!
    return vars.values.max()
}

fun solve8b(lines: List<String>): Int {

M src/main/advent2017/Day18.kt => src/main/advent2017/Day18.kt +1 -1
@@ 81,7 81,7 @@ class Duet(private val instructions: List<String>, val p: BigInteger = BigIntege

    private fun receive(x: String) {
        while (true) {
            if (receivedSounds.count() > 0) {
            if (receivedSounds.isNotEmpty()) {
                registers[x] = receivedSounds.remove()
                return
            }

M src/main/advent2017/Day24.kt => src/main/advent2017/Day24.kt +1 -1
@@ 58,7 58,7 @@ fun solve24a(input: List<String>): Int {

fun solve24b(input: List<String>): Int {
    val endBridges = getEndBridges(input)
    val maxLength = endBridges.map { it.builtFrom.count() }.maxOrNull()
    val maxLength = endBridges.maxOfOrNull { it.builtFrom.count() }
    return 1 + endBridges.filter { it.builtFrom.count() == maxLength }.maxOf { it.score() }
}


M src/main/advent2018/Day04.kt => src/main/advent2018/Day04.kt +1 -1
@@ 14,7 14,7 @@ class Guard(val id: Int) {
    val sleepiestMinute: Int
        get() = asleepTimes.map { it.second }.groupingBy { it }.eachCount().maxByOrNull { it.value }!!.key

    fun countAsleepAtMinute(minute: Int): Int = asleepTimes.filter { it.second == minute }.count()
    fun countAsleepAtMinute(minute: Int): Int = asleepTimes.count { it.second == minute }
}

fun getGuards(lines: List<String>): List<Guard> {

M src/main/advent2018/Day06.kt => src/main/advent2018/Day06.kt +1 -1
@@ 31,7 31,7 @@ fun solve6a(lines: List<String>): Int {
        }
    }

    return territory.values.filter { it in boundedZones }.groupingBy{ it }.eachCount().values.maxOrNull()!!
    return territory.values.filter { it in boundedZones }.groupingBy{ it }.eachCount().values.max()
}

fun solve6b(lines: List<String>, distanceThreshold: Int): Int {

M src/main/advent2018/Day09.kt => src/main/advent2018/Day09.kt +1 -1
@@ 29,7 29,7 @@ fun solve9a(numPlayers: Int, lastMarbleValue: Int): Long {
        currentMarble = nextMarble
        playerScores[marbleValue % numPlayers] = playerScores[marbleValue % numPlayers]!! + score
    }
    return playerScores.values.maxOrNull()!!
    return playerScores.values.max()
}

fun solve9b(numPlayers: Int, lastMarbleValue: Int): Long {

M src/main/advent2018/Day16.kt => src/main/advent2018/Day16.kt +1 -1
@@ 154,7 154,7 @@ fun solve16b(lines: List<String>): Int {
        uniqueMatches.forEach { (k, v) -> instructionMap[v.first()] = k }
        val toRemove = uniqueMatches.values.map { it.first() }
        reverseMatches.values.forEach {
            it.removeAll(toRemove)
            it.removeAll(toRemove.toSet())
        }

        // If a number appears only once in the set anywhere, we know what op maps to it

M src/main/advent2019/Day03.kt => src/main/advent2019/Day03.kt +3 -3
@@ 27,18 27,18 @@ fun wirePoints(wireInput: String): List<Point> {
}

fun solve3a(intersectionPoints: Set<Point>): Int {
    return intersectionPoints.map { it.abs }.filter { it > 0 }.minOrNull()!!
    return intersectionPoints.map { it.abs }.filter { it > 0 }.min()
}

fun solve3b(wire1Points: List<Point>, wire2Points: List<Point>, intersectionPoints: Set<Point>): Int {
    return intersectionPoints.map { wire1Points.indexOf(it) + wire2Points.indexOf(it) }.filter { it > 0 }.minOrNull()!!
    return intersectionPoints.map { wire1Points.indexOf(it) + wire2Points.indexOf(it) }.filter { it > 0 }.min()
}

fun main() {
    val input = input("day03")
    val wire1Points = wirePoints(input[0])
    val wire2Points = wirePoints(input[1])
    val intersectionPoints = wire1Points intersect wire2Points
    val intersectionPoints = wire1Points intersect wire2Points.toSet()
    println(solve3a(intersectionPoints)) // 5319
    println(solve3b(wire1Points, wire2Points, intersectionPoints)) // 12251
}
\ No newline at end of file

M src/main/advent2020/Day05.kt => src/main/advent2020/Day05.kt +1 -1
@@ 9,7 9,7 @@ fun solve5a(input: List<String>) = input.maxOf { seatId(it) }

fun solve5b(input: List<String>): Int {
    val seatIds = input.map { seatId(it) }
    return (seatIds.minOrNull()!!..seatIds.maxOrNull()!! subtract seatIds).first()
    return (seatIds.min()..seatIds.max() subtract seatIds.toSet()).first()
}

fun main() {

M src/main/advent2020/Day09.kt => src/main/advent2020/Day09.kt +1 -1
@@ 18,7 18,7 @@ fun solve9b(input: List<Long>, target: Long): Long {
        when {
            contiguousSum < target -> end += 1
            contiguousSum > target -> start += 1
            else -> return contiguousList.minOrNull()!! + contiguousList.maxOrNull()!!
            else -> return contiguousList.min() + contiguousList.max()
        }
    }
}

M src/main/advent2020/Day10.kt => src/main/advent2020/Day10.kt +2 -2
@@ 5,7 5,7 @@ import common.powerset
fun solve10a(joltages: List<Int>): Int {
    var ones = 0
    var threes = 0
    joltages.plus(0).plus(joltages.maxOrNull()!! + 3).sorted().windowed(2).forEach { (a, b) ->
    joltages.plus(0).plus(joltages.max() + 3).sorted().windowed(2).forEach { (a, b) ->
        if (b - a == 1) ones += 1
        if (b - a == 3) threes += 1
    }


@@ 26,7 26,7 @@ fun solve10b(joltages: List<Int>): Long {
        .filter { (a, b) -> b - a == 3 }
        .flatten()
        .plus(0)
        .plus(joltages.maxOrNull()!! + 3)
        .plus(joltages.max() + 3)
        .sorted()
        .windowed(2)
    return fixedJoltagePairs.map { (a, b) ->

M src/main/advent2020/Day13.kt => src/main/advent2020/Day13.kt +1 -1
@@ 57,7 57,7 @@ fun solve13b(input: List<String>): Long {
            it.second * bezout.first * i
        }.absoluteValue
    val solutions = countFrom().map { crtSolution - n * it }.takeWhileInclusive { it > 0 }
    return solutions.map { it.absoluteValue }.minOrNull()!!
    return solutions.map { it.absoluteValue }.min()
}

fun main() {

M src/main/advent2020/Day19.kt => src/main/advent2020/Day19.kt +1 -1
@@ 57,7 57,7 @@ fun solve19b(input: String): Int {
    rules[11] = "42 31 | 42 11 31"

    val messages = messagesText.split("\n")
    return messages.filter { matchesRuleZero(rules, it) }.count()
    return messages.count { matchesRuleZero(rules, it) }
}

fun main() {

M src/main/advent2020/Day23.kt => src/main/advent2020/Day23.kt +1 -1
@@ 53,7 53,7 @@ fun solve23a(initialCups: List<Int>): String {
}

fun solve23b(initialCups: List<Int>): Long {
    val oneMillionLabels = initialCups + ((initialCups.maxOrNull()!! + 1)..1_000_000)
    val oneMillionLabels = initialCups + ((initialCups.max() + 1)..1_000_000)
    val allCupsMap = cupSetup(oneMillionLabels)
    val firstCup = allCupsMap[initialCups.first()]!!
    val maxCup = 1_000_000

M src/main/advent2020/Day24.kt => src/main/advent2020/Day24.kt +1 -1
@@ 71,7 71,7 @@ fun solve24b(initialBlackTiles: Set<Point3D>): Int {
        }
        val whiteAdjTiles = blackTiles.flatMap { hexAdjacent(it) }.filter { it !in blackTiles }
        val whiteToBeFlipped = whiteAdjTiles.filter { hexAdjacent(it).count { adj -> adj in blackTiles } == 2 }
        blackTiles.minus(blackToBeFlipped).plus(whiteToBeFlipped)
        blackTiles.minus(blackToBeFlipped.toSet()).plus(whiteToBeFlipped)
    }
    return tileSequence.elementAt(100).count()
}

M src/main/advent2021/Day07.kt => src/main/advent2021/Day07.kt +4 -4
@@ 4,14 4,14 @@ import common.triangle
import kotlin.math.abs

fun solve7a(crabPositions: List<Int>): Int {
    val min = crabPositions.minOrNull()!!
    val max = crabPositions.maxOrNull()!!
    val min = crabPositions.min()
    val max = crabPositions.max()
    return (min..max).minOf { aligned -> crabPositions.sumOf { abs(it - aligned) } }
}

fun solve7b(crabPositions: List<Int>): Int {
    val min = crabPositions.minOrNull()!!
    val max = crabPositions.maxOrNull()!!
    val min = crabPositions.min()
    val max = crabPositions.max()
    return (min..max).minOf { aligned -> crabPositions.sumOf { triangle(abs(it - aligned)) } }
}


M src/main/advent2021/Day17.kt => src/main/advent2021/Day17.kt +0 -2
@@ 1,8 1,6 @@
package advent2021

import common.Point
import common.countFrom
import common.triangle
import kotlin.math.abs

private fun shootProbe(velocity: Point): Sequence<Point> {

M src/test/advent2019/Day03KtTest.kt => src/test/advent2019/Day03KtTest.kt +3 -3
@@ 8,19 8,19 @@ internal class Day03KtTest {
    val e1w2 = "U7,R6,D4,L4"
    val e1wp1 = wirePoints(e1w1)
    val e1wp2 = wirePoints(e1w2)
    val e1Intersect = e1wp1 intersect e1wp2
    val e1Intersect = e1wp1 intersect e1wp2.toSet()

    val e2w1 = "R75,D30,R83,U83,L12,D49,R71,U7,L72"
    val e2w2 = "U62,R66,U55,R34,D71,R55,D58,R83"
    val e2wp1 = wirePoints(e2w1)
    val e2wp2 = wirePoints(e2w2)
    val e2Intersect = e2wp1 intersect e2wp2
    val e2Intersect = e2wp1 intersect e2wp2.toSet()

    val e3w1 = "R98,U47,R26,D63,R33,U87,L62,D20,R33,U53,R51"
    val e3w2 = "U98,R91,D20,R16,D67,R40,U7,R15,U6,R7"
    val e3wp1 = wirePoints(e3w1)
    val e3wp2 = wirePoints(e3w2)
    val e3Intersect = e3wp1 intersect e3wp2
    val e3Intersect = e3wp1 intersect e3wp2.toSet()

    @Test
    fun examplesPartA() {