Skip to content

Commit

Permalink
2023 - Day 22 - input
Browse files Browse the repository at this point in the history
  • Loading branch information
fmmr committed Dec 22, 2023
1 parent 0aaf270 commit 97d2905
Show file tree
Hide file tree
Showing 7 changed files with 1,644 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/main/kotlin/no/rodland/advent/SpacePos.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import kotlin.math.*
internal const val ACTIVE = '#'
private fun Char.active() = this == ACTIVE

typealias Point = Pos

sealed class SpacePos {
abstract fun neighbours(): List<SpacePos>
abstract fun manhattan(): Int
Expand Down
61 changes: 61 additions & 0 deletions src/main/kotlin/no/rodland/advent_2023/Day21.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package no.rodland.advent_2023

import no.rodland.advent.Day
import no.rodland.advent.Point
import no.rodland.advent.Pos


Expand All @@ -9,8 +10,66 @@ import no.rodland.advent.Pos

class Day21(val input: List<String>) : Day<Int, Long, Map<Pos, Char>> {


private val parsed = input.parse()
private val width = parsed.keys.maxOf { it.x } + 1
private val height = parsed.keys.maxOf { it.y } + 1




// https://github.com/eagely/adventofcode/blob/main/src/main/kotlin/solutions/y2023/Day21.kt
fun solvePart2(): Any {
if (parsed.size < 50) return "womp womp"
val req = 26501365L
var delta = 0L
var skip = 0L
val queue = ArrayDeque<Pair<Point, Long>>()
val start = parsed.filterValues { it == 'S' }.keys.single()

queue.add(Pair(start, 0))
val visited = hashSetOf<Point>()
val size = Point(height, width)
val cycle = size.x * 2
var lastStep = 0L
var previousPlots = 0L
var delta1 = 0L
var delta2 = 0L
var plots = 0L
while (queue.isNotEmpty()) {
val (position, step) = queue.removeFirst()
if (position in visited) continue
if (step % 2 == 1L) visited.add(position)
if (step % cycle == 66L && step > lastStep) {
println("step: $step, plots: $plots, delta1: $delta1, delta2: $delta2")
lastStep = step
if (plots - previousPlots - delta1 == delta2) {
delta = plots - previousPlots + delta2
skip = step - 1
break
}
delta2 = (plots - previousPlots) - delta1
delta1 = plots - previousPlots
previousPlots = plots
}
plots = visited.size.toLong()
queue.addAll(position.neighbourCellsUDLR().filter { parsed[it % size] != '#' }.map { it to step + 1 })
}
while (skip < req) {
skip += cycle
plots += delta
delta += delta2
}
return plots
}

operator fun Pos.rem(other: Point) = Point(x pm other.x, y pm other.y)
operator fun Pos.rem(other: Int) = Point(x pm other, y pm other)
infix fun Pos.pm(other: Point) = this % other
infix fun Int.pm(other: Int): Int {
val mod = this % other
return if (mod < 0) mod + other else mod
}
override fun partOne(): Int {
// val start = parsed.filterValues { it == 'S' }.map { it.key }.single()
var newMap = parsed
Expand All @@ -31,6 +90,7 @@ class Day21(val input: List<String>) : Day<Int, Long, Map<Pos, Char>> {


override fun partTwo(): Long {
println(solvePart2())
return 2
}

Expand All @@ -44,3 +104,4 @@ class Day21(val input: List<String>) : Day<Int, Long, Map<Pos, Char>> {

override val day = "21".toInt()
}

27 changes: 27 additions & 0 deletions src/main/kotlin/no/rodland/advent_2023/Day22.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package no.rodland.advent_2023

import no.rodland.advent.Day

// template generated: 22/12/2023
// Fredrik Rødland 2023

class Day22(val input: List<String>) : Day<Long, Long, List<String>> {

private val parsed = input.parse()

override fun partOne(): Long {
return 2
}

override fun partTwo(): Long {
return 2
}

override fun List<String>.parse(): List<String> {
return map { line ->
line
}
}

override val day = "22".toInt()
}
3 changes: 2 additions & 1 deletion src/test/kotlin/no/rodland/advent_2023/Day21Test.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ internal class Day21Test {
resultTwo,
{ Day21(data21) },
{ Day21(test21) },
numTestPart1 = 2
numTestPart1 = 2,
numTestPart2 = 1,
)

@Nested
Expand Down
82 changes: 82 additions & 0 deletions src/test/kotlin/no/rodland/advent_2023/Day22Test.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package no.rodland.advent_2023

import no.rodland.advent.*
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import readFile

//
// run: download_aoc_input.sh to download input
//

@Suppress("ClassName")
@DisableSlow
internal class Day22Test {
private val data22 = "2023/input_22.txt".readFile()
private val test22 = "2023/input_22_test.txt".readFile()

private val resultTestOne = 2L
private val resultTestTwo = 2L
private val resultOne = 2L
private val resultTwo = 2L

val test = defaultTestSuiteParseOnInit(
Day22(data22),
Day22(test22),
resultTestOne,
resultOne,
resultTestTwo,
resultTwo,
{ Day22(data22) },
{ Day22(test22) },
)

@Nested
inner class Init {
@Test
fun `22,-,example,1`() {
report(AOCTest({ "123".toInt() }, Unit, 123, 5, "22".toInt(), Part.TWO, false, "example"))
}

@Test
fun `22,-,example,2`() {
report(test.initTest.copy())
}

@Test
fun `22,-,test,init`() {
report(test.initTest)
}

@Test
fun `22,-,live,init`() {
report(test.initLive)
}
}

@Nested
inner class `Part 1` {
@Test
fun `22,1,test`() {
report(test.testPart1)
}

@Test
fun `22,1,live,1`() {
report(test.livePart1)
}
}

@Nested
inner class `Part 2` {
@Test
fun `22,2,test`() {
report(test.testPart2)
}

@Test
fun `22,2,live,1`() {
report(test.livePart2)
}
}
}
Loading

0 comments on commit 97d2905

Please sign in to comment.