Guest User

Untitled

a guest
Dec 10th, 2023
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 5.74 KB | Source Code | 0 0
  1. class Pipes(private val pipes: List<String>) {
  2.     private val closedLoop = Array(pipes.size) { Array(pipes[0].length) { '.' } }
  3.     fun walkTheLoop(): Int {
  4.         val start = findStartPoint()
  5.         var direction = 0 to 1
  6.         var location = start + direction
  7.         var steps = 1
  8.         while (location != start) {
  9.             steps++
  10.             direction = goThroughPipe(location, direction)
  11.             location += direction
  12.         }
  13.         return steps / 2
  14.     }
  15.  
  16.     fun closeTheLoop(): Int {
  17.         val start = findStartPoint()
  18.         var direction = 0 to 1
  19.         var location = start + direction
  20.         var steps = 1
  21.         while (location != start) {
  22.             steps++
  23.             direction = goThroughPipe(location, direction)
  24.             closedLoop[location.first][location.second] = pipes[location.first][location.second]
  25. //            paintMap()
  26.             location += direction
  27.         }
  28.         closedLoop[start.first][start.second] = 'L'
  29. //        flood(closedLoopMap)
  30.         return paintMap()
  31.     }
  32.  
  33.     private fun paintMap(): Int {
  34.         var count = 0
  35.         val newMap = closedLoop.map {
  36.             var inside = false
  37.             var lastRelevantChar = '.'
  38.             it.map {
  39.                 when (it) {
  40.                     '.' -> {
  41.                         lastRelevantChar = '.'
  42.                         if (inside) {
  43.                             count++
  44.                             'I'
  45.                         } else 'O'
  46.                     }
  47.                     '|' -> {
  48.                         inside = !inside
  49.                         it
  50.                     }
  51.                     '-' -> it
  52.                     else -> {
  53.                         if (lastRelevantChar == '.') {
  54.                             lastRelevantChar = it
  55.                             inside = !inside
  56.                         } else {
  57.                             if (
  58.                                 lastRelevantChar == 'J' && it == 'L' ||
  59.                                 lastRelevantChar == 'L' && it == 'J' ||
  60.                                 lastRelevantChar == '7' && it == 'F' ||
  61.                                 lastRelevantChar == 'F' && it == '7'
  62.                             ) {
  63.                                 inside = !inside
  64.                             }
  65.                             lastRelevantChar = '.'
  66.                         }
  67.                         it
  68.                     }
  69.  
  70.                 }
  71.             }
  72.         }
  73.         val closedLoopMap = enhanceMap(newMap)
  74.         return count
  75.     }
  76.  
  77. //    private fun flood(closedLoopMap: List<String>): List<String> {
  78. //        val closedLoopArray = closedLoopMap.map { it.map { it }.toMutableList() }.toMutableList()
  79. //        recursiveFlood(closedLoopArray)
  80. //
  81. //    }
  82. //
  83. //    private fun recursiveFlood(closedLoopArray: MutableList<MutableList<Char>>, location: Pair<Int, Int> = 0 to 0) {
  84. //        closedLoopArray
  85. //    }
  86.  
  87.     private fun doNothing() {}
  88.     private fun enhanceMap(newMap: List<List<Any>>): List<String> {
  89.         return newMap.map { line ->
  90.             var top = ""
  91.             var center = ""
  92.             var bottom = ""
  93.             line.forEach { char ->
  94.                 when (char) {
  95.                     '|' -> {
  96.                         top += " █ "
  97.                         center += " █ "
  98.                         bottom += " █ "
  99.                     }
  100.                     '-' -> {
  101.                         top += "   "
  102.                         center += "███"
  103.                         bottom += "   "
  104.                     }
  105.                     'L' -> {
  106.                         top += " █ "
  107.                         center += " ██"
  108.                         bottom += "   "
  109.                     }
  110.                     'J' -> {
  111.                         top += " █ "
  112.                         center += "██ "
  113.                         bottom += "   "
  114.                     }
  115.                     '7' -> {
  116.                         top += "   "
  117.                         center += "██ "
  118.                         bottom += " █ "
  119.                     }
  120.                     'F' -> {
  121.                         top += "   "
  122.                         center += " ██"
  123.                         bottom += " █ "
  124.                     }
  125.                     else -> {
  126.                         top += "   "
  127.                         center += " $char "
  128.                         bottom += "   "
  129.                     }
  130.                 }
  131.             }
  132.             println(top + "\n" + center + "\n" + bottom)
  133.             top + "\n" + center + "\n" + bottom
  134.         }
  135.     }
  136.  
  137.     private fun goThroughPipe(
  138.         location: Pair<Int, Int>,
  139.         direction: Pair<Int, Int>
  140.     ): Pair<Int, Int> {
  141.         var direction1 = direction
  142.         return when (pipes[location.first][location.second]) {
  143.             '|' -> direction1
  144.             '-' -> direction1
  145.             'L' -> {
  146.                 direction1 = if (direction1 == 1 to 0) 0 to 1 else -1 to 0
  147.                 direction1
  148.             }
  149.             'J' -> {
  150.                 direction1 = if (direction1 == 1 to 0) 0 to -1 else -1 to 0
  151.                 direction1
  152.             }
  153.             '7' -> {
  154.                 direction1 = if (direction1 == -1 to 0) 0 to -1 else 1 to 0
  155.                 direction1
  156.             }
  157.             else -> {
  158.                 direction1 = if (direction1 == -1 to 0) 0 to 1 else 1 to 0
  159.                 direction1
  160.             }
  161.         }
  162.     }
  163.  
  164.     private fun findStartPoint(): Pair<Int, Int> {
  165.         val y = pipes.indexOfFirst { it.contains('S') }
  166.         return y to pipes[y].indexOf('S')
  167.     }
  168. }
  169.  
  170. private operator fun Pair<Int, Int>.plus(other: Pair<Int, Int>): Pair<Int, Int> =
  171.     this.first + other.first to this.second + other.second
Advertisement
Add Comment
Please, Sign In to add comment