diff --git a/master/src/test/kotlin/days/Day10.kt b/master/src/test/kotlin/days/Day10.kt index 650eedc..9bb0814 100644 --- a/master/src/test/kotlin/days/Day10.kt +++ b/master/src/test/kotlin/days/Day10.kt @@ -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 @@ -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) @@ -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>, + loop: List> + ): Set> { + + 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) } } - diff --git a/master/src/test/resources/days/10/fill1.txt b/master/src/test/resources/days/10/fill1.txt new file mode 100644 index 0000000..5222073 --- /dev/null +++ b/master/src/test/resources/days/10/fill1.txt @@ -0,0 +1,5 @@ +F-S-7 +|...| +|...| +|...| +L---J \ No newline at end of file diff --git a/master/src/test/resources/days/10/stest1.txt b/master/src/test/resources/days/10/stest1.txt new file mode 100644 index 0000000..ce26ab7 --- /dev/null +++ b/master/src/test/resources/days/10/stest1.txt @@ -0,0 +1,5 @@ +..... +F-S-7 +|...| +|...| +L---J \ No newline at end of file