Skip to content

Commit

Permalink
10 done
Browse files Browse the repository at this point in the history
  • Loading branch information
rich.blockley committed Mar 24, 2024
1 parent 6b99b87 commit ed7c389
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 43 deletions.
114 changes: 71 additions & 43 deletions master/src/test/kotlin/days/Day10.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package days

import assertk.assertThat
import assertk.assertions.contains
import assertk.assertions.hasSameSizeAs
import assertk.assertions.hasSize
import assertk.assertions.isEqualTo
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.CsvSource
Expand Down Expand Up @@ -185,19 +185,27 @@ class Day10 {
Grid.Position.T to listOf(Grid.Position.R),
Grid.Position.B to listOf(Grid.Position.L)
),
'S' to mapOf(
Grid.Position.B to listOf(Grid.Position.L),
Grid.Position.T to listOf(Grid.Position.R),
Grid.Position.L to listOf(Grid.Position.T),
Grid.Position.R to listOf(Grid.Position.B),
),
)


@ParameterizedTest
@CsvSource(
value = [
//"src/test/resources/days/10/samp3.txt, 4",
//"src/test/resources/days/10/samp4.txt, 8",
//"src/test/resources/days/10/samp5.txt, 10",
"src/test/resources/days/10/prod1.txt, -1"
"src/test/resources/days/10/samp3.txt, false, 4",
"src/test/resources/days/10/samp4.txt, true, 8",
"src/test/resources/days/10/samp5.txt, true, 10",
"src/test/resources/days/10/stest1.txt, true, 6",
"src/test/resources/days/10/fill1.txt, true, 9",
"src/test/resources/days/10/prod1.txt, true, 343"
]
)
fun day10Question2(inputFile: String, expected: Int) {
fun day10Question2(inputFile: String, left: Boolean, expected: Int) {

val inputLines = filePathToLines(inputFile)

Expand Down Expand Up @@ -278,65 +286,85 @@ class Day10 {

return loop.flatMapIndexed { i, vElem ->

if (i == 0) {
emptyList()
} else {
val vVal = vElem.value()

val vVal = vElem.value()
val uElem = if (i > 0) loop[i - 1] else loop[loop.lastIndex]

val uElem = loop[i - 1]
val positionOfPrevious =
if (uElem.x < vElem.x)
Grid.Position.L
else if (uElem.x > vElem.x)
Grid.Position.R
else if (uElem.y < vElem.y)
Grid.Position.T
else if (uElem.y > vElem.y)
Grid.Position.B
else
throw Error("Cannot identify position of uElem relative to vElem")

val charToLookForLookingFor = lookLocationsMap[vVal]!!
val placesToLook = charToLookForLookingFor[positionOfPrevious]!!

val positionOfPrevious =
if (uElem.x < vElem.x)
Grid.Position.L
else if (uElem.x > vElem.x)
Grid.Position.R
else if (uElem.y < vElem.y)
Grid.Position.T
else if (uElem.y > vElem.y)
Grid.Position.B
else
throw Error("Cannot identify position of uElem relative to vElem")
val lookingAts = vElem.neighboursExc().filter { it.position in placesToLook }

val charToLookForLookingFor = lookLocationsMap[vVal]!!
val placesToLook = charToLookForLookingFor[positionOfPrevious]!!
lookingAts.flatMap {
if (it !in loop)
listOf(it)
else
emptyList()
}

val lookingAts = vElem.neighboursExc().filter { it.position in placesToLook }
}
}

lookingAts.flatMap {
if (it !in loop)
tailrec fun fillFill(
data: Set<Grid.GridElem<Char>>,
loop: List<Grid.GridElem<Char>>
): Set<Grid.GridElem<Char>> {

val dataGrid = gridFromSparse(grid.width, grid.height, '*', '-', data)

val newFills = dataGrid
.filter { it.value() == '*' }
.flatMap {
val neighbs = it.neighboursExc().filter {
it.position in listOf(
Grid.Position.T,
Grid.Position.L,
Grid.Position.B,
Grid.Position.R
)
}
neighbs.flatMap {
if (it !in loop && it !in data)
listOf(it)
else
emptyList()
}
}
}

if (newFills.isEmpty())
return data
else
return fillFill(data + newFills, loop)
}

val enclosedLeft = findEnclosedLeftSimple(
val directedLoop = if (left)
loop
).toSet()

val enclosedRight = findEnclosedLeftSimple(
else
listOf(loop[0]) + (loop.subList(1, loop.size).asReversed())
)
.toSet()

gridFromSparse(grid.width, grid.height, '#', '-', enclosedLeft)
.printGrid()
val enclosed = findEnclosedLeftSimple(directedLoop).toSet()

println()
println()
val filledEnclosed = fillFill(enclosed, directedLoop)

gridFromSparse(grid.width, grid.height, '#', '-', enclosedRight)
gridFromSparse(grid.width, grid.height, '#', '-', filledEnclosed)
.printGrid()

println(enclosedLeft.size)
println(enclosedRight.size)
println()
println()

assertThat(listOf(enclosedLeft.size, enclosedRight.size)).contains(expected)
assertThat(filledEnclosed).hasSize(expected)
}

}

5 changes: 5 additions & 0 deletions master/src/test/resources/days/10/fill1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
F-S-7
|...|
|...|
|...|
L---J
5 changes: 5 additions & 0 deletions master/src/test/resources/days/10/stest1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.....
F-S-7
|...|
|...|
L---J

0 comments on commit ed7c389

Please sign in to comment.