Slept in, didn't hurry to finish Day 10. Considering the solution elegant regarding the traversal of the pipes.

This commit is contained in:
Chris Hodges 2023-12-10 11:31:09 +01:00
parent 56fcbfe165
commit b98653f2b9
2 changed files with 278 additions and 1 deletions

View File

@ -140,13 +140,25 @@ class CharGrid {
return newGrid return newGrid
} }
fun collect(c: Int, r: Int, relposes: Iterable<RelPos>, skipBorder: Boolean = true): List<Char> = fun collectRelative(c: Int, r: Int, relposes: Iterable<RelPos>, skipBorder: Boolean = true): List<Char> =
if (skipBorder) { if (skipBorder) {
relposes.mapNotNull { getOrNull(c + it.dc, r + it.dr) } relposes.mapNotNull { getOrNull(c + it.dc, r + it.dr) }
} else { } else {
relposes.map { get(c + it.dc, r + it.dr) } relposes.map { get(c + it.dc, r + it.dr) }
} }
fun findMatches(predicate: (char: Char) -> Boolean): List<Pair<Int, Int>> {
val matches = ArrayList<Pair<Int, Int>>()
for (r in 0 until height) {
for (c in 0 until width) {
if (predicate(get(c, r))) {
matches.add(c to r)
}
}
}
return matches
}
fun addBorder(borderChar: Char = bChar): CharGrid { fun addBorder(borderChar: Char = bChar): CharGrid {
val topBottom = CharArray(width + 2) { borderChar } val topBottom = CharArray(width + 2) { borderChar }
return CharGrid(Array(height + 2) { return CharGrid(Array(height + 2) {

265
src/aoc2023/Day10.kt Normal file
View File

@ -0,0 +1,265 @@
package aoc2023
import CharGrid
import println
import readInput
/*
--- Day 10: Pipe Maze ---
You use the hang glider to ride the hot air from Desert Island all the way up to the floating metal island. This island is surprisingly cold and there definitely aren't any thermals to glide on, so you leave your hang glider behind.
You wander around for a while, but you don't find any people or animals. However, you do occasionally find signposts labeled "Hot Springs" pointing in a seemingly consistent direction; maybe you can find someone at the hot springs and ask them where the desert-machine parts are made.
The landscape here is alien; even the flowers and trees are made of metal. As you stop to admire some metal grass, you notice something metallic scurry away in your peripheral vision and jump into a big pipe! It didn't look like any animal you've ever seen; if you want a better look, you'll need to get ahead of it.
Scanning the area, you discover that the entire field you're standing on is densely packed with pipes; it was hard to tell at first because they're the same metallic silver color as the "ground". You make a quick sketch of all of the surface pipes you can see (your puzzle input).
The pipes are arranged in a two-dimensional grid of tiles:
| is a vertical pipe connecting north and south.
- is a horizontal pipe connecting east and west.
L is a 90-degree bend connecting north and east.
J is a 90-degree bend connecting north and west.
7 is a 90-degree bend connecting south and west.
F is a 90-degree bend connecting south and east.
. is ground; there is no pipe in this tile.
S is the starting position of the animal; there is a pipe on this tile, but your sketch doesn't show what shape the pipe has.
Based on the acoustics of the animal's scurrying, you're confident the pipe that contains the animal is one large, continuous loop.
For example, here is a square loop of pipe:
.....
.F-7.
.|.|.
.L-J.
.....
If the animal had entered this loop in the northwest corner, the sketch would instead look like this:
.....
.S-7.
.|.|.
.L-J.
.....
In the above diagram, the S tile is still a 90-degree F bend: you can tell because of how the adjacent pipes connect to it.
Unfortunately, there are also many pipes that aren't connected to the loop! This sketch shows the same loop as above:
-L|F7
7S-7|
L|7||
-L-J|
L|-JF
In the above diagram, you can still figure out which pipes form the main loop: they're the ones connected to S, pipes those pipes connect to, pipes those pipes connect to, and so on. Every pipe in the main loop connects to its two neighbors (including S, which will have exactly two pipes connecting to it, and which is assumed to connect back to those two pipes).
Here is a sketch that contains a slightly more complex main loop:
..F7.
.FJ|.
SJ.L7
|F--J
LJ...
Here's the same example sketch with the extra, non-main-loop pipe tiles also shown:
7-F7-
.FJ|7
SJLL7
|F--J
LJ.LJ
If you want to get out ahead of the animal, you should find the tile in the loop that is farthest from the starting position. Because the animal is in the pipe, it doesn't make sense to measure this by direct distance. Instead, you need to find the tile that would take the longest number of steps along the loop to reach from the starting point - regardless of which way around the loop the animal went.
In the first example with the square loop:
.....
.S-7.
.|.|.
.L-J.
.....
You can count the distance each tile in the loop is from the starting point like this:
.....
.012.
.1.3.
.234.
.....
In this example, the farthest point from the start is 4 steps away.
Here's the more complex loop again:
..F7.
.FJ|.
SJ.L7
|F--J
LJ...
Here are the distances for each tile on that loop:
..45.
.236.
01.78
14567
23...
Find the single giant loop starting at S. How many steps along the loop does it take to get from the starting position to the point farthest from the starting position?
*/
fun main() {
val inlineTestInput = """
..F7.
.FJ|.
SJ.L7
|F--J
LJ...
"""
val inlineTestInput2a = """
.F----7F7F7F7F-7....
.|F--7||||||||FJ....
.||.FJ||||||||L7....
FJL7L7LJLJ||LJ.L-7..
L--J.L7...LJS7F-7L7.
....F-J..F7FJ|L7L7L7
....L7.F7||L7|.L7L7|
.....|FJLJ|FJ|F7|.LJ
....FJL-7.||.||||...
....L---J.LJ.LJLJ...
"""
val inlineTestInput2 = """
FF7FSF7F7F7F7F7F---7
L|LJ||||||||||||F--J
FL-7LJLJ||||||LJL-77
F--JF--7||LJLJ7F7FJ-
L---JF-JLJ.||-FJLJJ7
|F|F-JF---7F7-L7L|7|
|FFJF7L7F-JF7|JL---7
7-L-JL7||F7|L7F-7F7|
L.L7LFJ|||||FJL7||LJ
L7JLJL-JLJLJL--JLJ.L
"""
val north = 1
val south = 2
val west = 4
val east = 8
val dirMask = north or south or west or east
val start = 16
val directions = mapOf(
north to (0 to -1),
south to (0 to 1),
west to (-1 to 0),
east to (1 to 0)
)
val mapping = mapOf(
'S' to start,
'F' to (south or east),
'7' to (west or south),
'J' to (north or west),
'L' to (east or north),
'-' to (west or east),
'|' to (north or south),
'.' to 0
)
fun swapDirections(code: Int) = ((code and (north or west)) shl 1) or ((code and (south or east)) shr 1)
fun part1(input: List<String>): Int {
val grid = CharGrid(input)
val startPos = grid.findMatches { it == 'S' }.first()
grid.apply { (mapping[it]!! + 32).toChar() }
val currentDirs = directions.filter {
(grid[startPos.first + it.value.first, startPos.second + it.value.second].code and swapDirections(it.key)) != 0
}.keys.toTypedArray()
val locations = arrayOf(startPos, startPos)
var distance = 0
var notHit = true
do {
distance++
for (i in locations.indices) {
val dir = currentDirs[i]
val dirRel = directions[dir]!!
val loc = locations[i]
val newLoc = loc.first + dirRel.first to loc.second + dirRel.second
if (grid[newLoc.first, newLoc.second] == 'Z') {
notHit = false
break
}
currentDirs[i] = (grid[newLoc.first, newLoc.second].code xor swapDirections(dir)) and dirMask
locations[i] = newLoc
grid[newLoc.first, newLoc.second] = 'Z'
}
} while (notHit)
return distance
}
fun floodFill(grid: Array<IntArray>, c: Int, r: Int) {
grid[r][c] = 2
CharGrid.PLUS_POS.map { it.translate(c, r) }
// .filter { it.dc in grid[0].indices && it.dr in grid.indices }
.filter { grid[it.dr][it.dc] == 0 }.forEach { floodFill(grid, it.dc, it.dr) }
}
fun part2(input: List<String>): Int {
val grid = CharGrid(input)
val startPos = grid.findMatches { it == 'S' }.first()
grid.apply { (mapping[it]!! + 32).toChar() }
val currentDirs = directions.filter {
(grid[startPos.first + it.value.first, startPos.second + it.value.second].code and swapDirections(it.key)) != 0
}.keys.toTypedArray()
val outline = Array(grid.height * 3) { IntArray(grid.width * 3) }
val locations = arrayOf(startPos, startPos)
var notHit = true
do {
for (i in locations.indices) {
val dir = currentDirs[i]
val dirRel = directions[dir]!!
val loc = locations[i]
outline[loc.second * 3 + 1][loc.first * 3 + 1] = 1
outline[loc.second * 3 + 1 + dirRel.second][loc.first * 3 + 1 + dirRel.first] = 1
outline[loc.second * 3 + 1 + dirRel.second * 2][loc.first * 3 + 1 + dirRel.first * 2] = 1
val newLoc = loc.first + dirRel.first to loc.second + dirRel.second
if (grid[newLoc.first, newLoc.second] == 'Z') {
notHit = false
break
}
val newDir = (grid[newLoc.first, newLoc.second].code xor swapDirections(dir)) and dirMask
val newDirRel = directions[newDir]!!
outline[newLoc.second * 3 + 1][newLoc.first * 3 + 1] = 1
outline[newLoc.second * 3 + 1 + newDirRel.second][newLoc.first * 3 + 1 + newDirRel.first] = 1
currentDirs[i] = newDir
locations[i] = newLoc
grid[newLoc.first, newLoc.second] = 'Z'
}
} while (notHit)
// guess the inside
val ipos = 1 to 1
if (outline[startPos.second * 3 + 1 + ipos.second][startPos.first * 3 + 1 + ipos.first] != 0) {
println("Bah!")
}
floodFill(outline, startPos.first * 3 + 1 + ipos.first, startPos.second * 3 + 1 + ipos.second)
var insideCount = 0
var outsideCount = 0
grid.applyWithPos { grid, col, row ->
if (grid[col, row] != 'Z') {
if (outline[row * 3][col * 3] == 2) insideCount++ else outsideCount++
}
null
}
return insideCount
}
// test if implementation meets criteria from the description, like:
val testInput = inlineTestInput.trim().reader().readLines()
val testInput2 = inlineTestInput2.trim().reader().readLines()
//val testInput = readInput("aoc2023/Day10_test")
val testInputPart1Result = part1(testInput)
println("Part 1 Test: $testInputPart1Result")
//val testInputPart2Result = part2(testInput2)
//println("Part 2 Test: $testInputPart2Result")
check(testInputPart1Result == 8)
//check(testInputPart2Result == 10)
val input = readInput("aoc2023/Day10")
part1(input).println()
part2(input).println()
}