Skip to content

Commit

Permalink
2023/day 14 - part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
underlow committed Dec 14, 2023
1 parent 08b84fa commit 940298b
Show file tree
Hide file tree
Showing 2 changed files with 184 additions and 14 deletions.
138 changes: 125 additions & 13 deletions src/main/kotlin/me/underlow/advent2023/Day14.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,15 @@ object ParabolicReflectorDish {
fun part1(list: List<String>): Int {
val input = parseInput(list)

for (row in input.indices) {
for (column in input[0].indices) {
north(input)

return load(input)
}

// up
fun north(input: Array<Array<Char>>) {
for (column in input[0].indices) {
for (row in input.indices) {
if (input[row][column] == 'O') {
// move up
for (rowUp in row downTo 1) {
Expand All @@ -23,35 +30,140 @@ object ParabolicReflectorDish {
}
}
}
var sum = 0
input.dump()
}

//down
fun south(input: Array<Array<Char>>) {
for (column in input[0].indices) {
for (row in input.indices.reversed()) {
if (input[row][column] == 'O') {
// move down
for (rowMove in row until input.size - 1) {
if (input[rowMove + 1][column] == '.') {
input[rowMove + 1][column] = 'O'
input[rowMove][column] = '.'
} else {
break
}
}
}
}
}
}

//left
fun west(input: Array<Array<Char>>) {
for (row in input.indices) {
for (column in input[0].indices) {
if (input[row][column] == 'O') {
sum += input.size - row
// move left
for (colMove in column downTo 1) {
if (input[row][colMove - 1] == '.') {
input[row][colMove - 1] = 'O'
input[row][colMove] = '.'
} else {
break
}
}
}
}
}
}

return sum
// right
fun east(input: Array<Array<Char>>) {
for (row in input.indices) {
for (column in input[0].indices.reversed()) {
if (input[row][column] == 'O') {
// move right
for (colMove in column until input.size - 1) {
if (input[row][colMove + 1] == '.') {
input[row][colMove + 1] = 'O'
input[row][colMove] = '.'
} else {
break
}
}
}
}
}
}

fun Array<Array<Char>>.dump() {
for (column in this[0].indices) {
for (row in this.indices) {
println()
for (row in this.indices) {
for (column in this[0].indices) {
print(this[row][column])
}
println()
}
}


fun part2(list: List<String>): Int {
val directions = parseInput(list)
return 0
fun part2(list: List<String>, count: Long = 1_000_000_000): Int {
val input = parseInput(list)
val results = mutableListOf<Int>()
for (i in 0 until count) {
north(input)
west(input)
south(input)
east(input)

val sum = load(input)
// if (results.contains(sum))
// println("$i -> $sum")

results.add(sum)

val c = findCycle(results)
if (c != null && c.second > 1) {
val cStart = c.first
val cLen = c.second

val rem = count % cLen
val a = results[((count - cStart) % cLen).toInt() + cStart - 1]
return a
}

}

return load(input)
}

fun findCycle(results: List<Int>): Pair<Int, Int>? {
// looking for a cycle filling list at least two times
// lkfjerlkfj|aabbcc|aabbcc

for (i in results.indices) {
if ((results.size - i) % 2 != 0)
continue

var cycle = true
for (j in 0 until (results.size - i) / 2) {
if (results[j + i] != results[i + (results.size - i) / 2 + j])
cycle = false
}
if (cycle)
return i to (results.size - i) / 2
}

return null
}

private fun load(input: Array<Array<Char>>): Int {
var sum = 0
for (row in input.indices) {
for (column in input[0].indices) {
if (input[row][column] == 'O') {
sum += input.size - row
}
}
}

return sum
}

private fun parseInput(list: List<String>): Array<Array<Char>> {
fun parseInput(list: List<String>): Array<Array<Char>> {
return list.map {
it.toCharArray().toTypedArray()
}.toTypedArray()
Expand All @@ -68,5 +180,5 @@ fun main() {
println("part 2: $res2")

checkResult(res1, 112773)
checkResult(res2, 0)
checkResult(res2, 98894)
}
60 changes: 59 additions & 1 deletion src/test/kotlin/me/underlow/advent2023/Day14Test.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
package me.underlow.advent2023

import me.underlow.advent2023.ParabolicReflectorDish.dump
import org.junit.jupiter.api.Assertions.assertArrayEquals
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test

class ParabolicReflectorDishTest {
@Test
fun findCycleTest() {
val l = listOf(1, 2, 3, 4, 5, 6, 7, 4, 5, 6, 7)
val r = ParabolicReflectorDish.findCycle(l)
assertEquals(3 to 4, r)
val l2 = listOf(4, 5, 6, 7, 4, 5, 6, 7)
val r2 = ParabolicReflectorDish.findCycle(l2)
assertEquals(0 to 4, r2)
val l3 = listOf(1, 2, 4, 5, 6, 7, 4, 5, 6, 7)
val r3 = ParabolicReflectorDish.findCycle(l3)
assertEquals(2 to 4, r3)
}

@Test
fun testPart1() {
val result = ParabolicReflectorDish.part1(input.split("\n"))
Expand All @@ -13,7 +28,38 @@ class ParabolicReflectorDishTest {
@Test
fun testPart2() {
val result = ParabolicReflectorDish.part2(input.split("\n"))
assertEquals(0, result)
assertEquals(64, result)
}

@Test
fun testPart22() {
val input = ParabolicReflectorDish.parseInput(input.split("\n"))
val output = ParabolicReflectorDish.parseInput(c1.split("\n"))
for (i in 0 until 1) {
println("start")
input.dump()
ParabolicReflectorDish.north(input)
println("north")
input.dump()
ParabolicReflectorDish.west(input)
println("west")
input.dump()
ParabolicReflectorDish.south(input)
println("south")
input.dump()
ParabolicReflectorDish.east(input)
println("east")
input.dump()

}
input.dump()
println()
output.dump()
assertArrayEquals(output, input)
}

@Test
fun findCycle() {
}
}

Expand All @@ -29,3 +75,15 @@ private val input = """
#....###..
#OO..#....
""".trimIndent()
private val c1 = """
.....#....
....#...O#
...OO##...
.OO#......
.....OOO#.
.O#...O#.#
....O#....
......OOOO
#...O###..
#..OO#....
""".trimIndent()

0 comments on commit 940298b

Please sign in to comment.