Advertisement
Guest User

Untitled

a guest
Dec 16th, 2023
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 KB | Source Code | 0 0
  1. fun part1() {
  2. val amountOfEnergy = energizeLavaField(input)
  3. println("Part 1: " + amountOfEnergy)
  4. }
  5.  
  6. fun part2() {
  7. val amountOfEnergy = bestEnergizeLavaField(input)
  8. println("Part 2: " + amountOfEnergy)
  9. }
  10.  
  11. fun energizeLavaField(mirrorMap: List<String>): Int {
  12. val energizedField = bounceRay(mirrorMap, 0, 0, Right)
  13. return energizedField.size
  14. }
  15.  
  16. fun bestEnergizeLavaField(mirrorMap: List<String>): Int {
  17. val bottom = mirrorMap.size -1
  18. val right = mirrorMap[0].length -1
  19. val horizontalEdgeMax = (0..right).map { x ->
  20. maxOf(bounceRay(mirrorMap, x, 0, Down).size, bounceRay(mirrorMap, x, bottom, Up).size)
  21. }.max() ?: 0
  22. val verticalEdgeMax = (0..bottom).map { y ->
  23. maxOf(bounceRay(mirrorMap, 0, y, Right).size, bounceRay(mirrorMap, right, y, Left).size)
  24. }.max() ?: 0
  25. return maxOf(horizontalEdgeMax, verticalEdgeMax)
  26. }
  27.  
  28. private fun bounceRay(
  29. mirrorMap: List<String>,
  30. startX: Int,
  31. startY: Int,
  32. startDir: Direction,
  33. visitedTiles: MutableSet<Triple<Int, Int, Direction>> = mutableSetOf()
  34. ): Set<Pair<Int, Int>> {
  35. if (outOfBounds(startX, startY, startDir, mirrorMap)) return emptySet()
  36. val energizedField = mutableSetOf<Pair<Int, Int>>()
  37. var x = startX
  38. var y = startY
  39. var direction = startDir
  40. do {
  41. energizedField += x to y
  42. val currentTile = mirrorMap[y][x]
  43. if (currentTile != '.') {
  44. if (visitedTiles.contains(Triple(x, y, direction))) return energizedField
  45. visitedTiles.add(Triple(x, y, direction))
  46. when (direction) {
  47. Right -> {
  48. when (currentTile) {
  49. '\\' -> direction = Down
  50. '/' -> direction = Up
  51. '|' -> {
  52. direction = Up
  53. energizedField += bounceRay(mirrorMap, x, y + 1, Down, visitedTiles)
  54. }
  55. }
  56. }
  57. Left -> {
  58. when (currentTile) {
  59. '\\' -> direction = Up
  60. '/' -> direction = Down
  61. '|' -> {
  62. direction = Up
  63. energizedField += bounceRay(mirrorMap, x, y + 1, Down, visitedTiles)
  64. }
  65. }
  66. }
  67. Down -> {
  68. when (currentTile) {
  69. '\\' -> direction = Right
  70. '/' -> direction = Left
  71. '-' -> {
  72. direction = Left
  73. energizedField += bounceRay(mirrorMap, x + 1, y, Right, visitedTiles)
  74. }
  75. }
  76. }
  77. Up -> {
  78. when (currentTile) {
  79. '\\' -> direction = Left
  80. '/' -> direction = Right
  81. '-' -> {
  82. direction = Left
  83. energizedField += bounceRay(mirrorMap, x + 1, y, Right, visitedTiles)
  84. }
  85. }
  86. }
  87. }
  88. }
  89. val move = moveInDirection(x, y, direction)
  90. x = move.first
  91. y = move.second
  92. } while (!outOfBounds(x, y, direction, mirrorMap))
  93. return energizedField
  94. }
  95.  
  96.  
  97. private fun moveInDirection(x: Int, y: Int, direction: Direction) =
  98. when (direction) {
  99. Right -> x + 1 to y
  100. Left -> x - 1 to y
  101. Up -> x to y - 1
  102. Down -> x to y + 1
  103. }
  104.  
  105. private fun outOfBounds(x: Int, y: Int, dir: Direction, map: List<String>) =
  106. when (dir) {
  107. Right -> x == map[0].length
  108. Left -> x == -1
  109. Up -> y == -1
  110. Down -> y == map.size
  111. }
  112.  
  113. enum class Direction {
  114. Right, Down, Left, Up
  115. }
Tags: adventofcode
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement