vbe_elvis

2021 Day 11

Dec 11th, 2021
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 4.93 KB | None | 0 0
  1. class Octopus(var energyLevel: Int) {
  2.     var surroundingOctopuses: List<Octopus> = emptyList()
  3.  
  4.     fun increaseEnergy() {
  5.         energyLevel++
  6.         if (energyLevel == 10)
  7.             surroundingOctopuses.forEach { it.increaseEnergy() }
  8.     }
  9.  
  10.     fun flash(): Int {
  11.         if (energyLevel < 10 ) return 0
  12.         energyLevel = 0
  13.         return 1
  14.     }
  15. }
  16.  
  17. class OctopusCave {
  18.     @Test
  19.     fun `Should increase energy one at a time`() {
  20.         val octopus = Octopus(1)
  21.         octopus.increaseEnergy()
  22.         assertEquals(2, octopus.energyLevel)
  23.         octopus.increaseEnergy()
  24.         assertEquals(3, octopus.energyLevel)
  25.     }
  26.  
  27.     @Test
  28.     fun `Should drop to zero on flash`() {
  29.         val octopus = Octopus(10)
  30.         octopus.flash()
  31.         assertEquals(0, octopus.energyLevel)
  32.     }
  33.  
  34.     @Test
  35.     fun `Should reset to 0 if flashing above 9 energy`() {
  36.         val octopus = Octopus(7)
  37.         octopus.flash()
  38.         assertEquals(7, octopus.energyLevel)
  39.         octopus.increaseEnergy()
  40.         octopus.flash()
  41.         assertEquals(8, octopus.energyLevel)
  42.         octopus.increaseEnergy()
  43.         octopus.flash()
  44.         assertEquals(9, octopus.energyLevel)
  45.         octopus.increaseEnergy()
  46.         octopus.flash()
  47.         assertEquals(0, octopus.energyLevel)
  48.     }
  49.  
  50.     @Test
  51.     fun `Should increase adjacent energy when increasing above 9`() {
  52.         val octopus = Octopus(9)
  53.         val neighbor1 = Octopus(5)
  54.         val neighbor2 = Octopus(9)
  55.         val neighbor3 = Octopus(4)
  56.         val someOther = Octopus(7)
  57.         octopus.surroundingOctopuses = listOf(neighbor1, neighbor2, neighbor3)
  58.         neighbor1.surroundingOctopuses = listOf(octopus, neighbor2, someOther)
  59.         neighbor2.surroundingOctopuses = listOf(octopus, neighbor1)
  60.         octopus.increaseEnergy()
  61.         assertEquals(11, octopus.energyLevel)
  62.         assertEquals(7, neighbor1.energyLevel)
  63.         assertEquals( 10, neighbor2.energyLevel)
  64.         assertEquals( 5, neighbor3.energyLevel)
  65.         assertEquals( 7, someOther.energyLevel)
  66.     }
  67.  
  68.     @Test
  69.     fun `Small example single step` () {
  70.         assertEquals(9, smallExampleCave.step())
  71.         assertEquals(0, smallExampleCave.step())
  72.     }
  73.  
  74.     @Test
  75.     fun `Large example 10 steps` () {
  76.         var totalFlashes = 0L
  77.         repeat(10) { totalFlashes += largeExampleCave.step() }
  78.         assertEquals(204, totalFlashes)
  79.     }
  80.  
  81.     @Test
  82.     fun `Large example 100 steps` () {
  83.         var totalFlashes = 0L
  84.         repeat(100) { totalFlashes += largeExampleCave.step() }
  85.         assertEquals(1656, totalFlashes)
  86.     }
  87.  
  88.     @Test
  89.     fun `Part 1` () {
  90.         var totalFlashes = 0L
  91.         repeat(100) { totalFlashes += puzzleCave.step() }
  92.         println("Part 1: $totalFlashes")
  93.     }
  94.  
  95.     @Test
  96.     fun `First synchronized flash` () {
  97.         var step = 1
  98.         while (largeExampleCave.step() != largeExampleCave.size) step++
  99.         assertEquals(195, step)
  100.     }
  101.  
  102.     @Test
  103.     fun `Part 2` () {
  104.         var step = 1
  105.         while (puzzleCave.step() != puzzleCave.size) step++
  106.         println("Part 2: $step")
  107.     }
  108.  
  109.     private fun List<List<Octopus>>.withSurrounding(): List<Octopus> {
  110.         this.forEachIndexed { y, row ->
  111.             row.forEachIndexed{ x, octopus ->
  112.                 octopus.surroundingOctopuses = surrounding(x, y)
  113.             }
  114.         }
  115.         return this.flatten();
  116.     }
  117.  
  118.     private fun List<List<Octopus>>.surrounding(x: Int, y: Int): List<Octopus> {
  119.         var list = emptyList<Octopus>().toMutableList()
  120.         // Three octopuses above
  121.         if (y != 0) {
  122.             if (x != 0) list.add(this[y-1][x-1])
  123.             list.add(this[y-1][x])
  124.             if (x != this[0].size-1) list.add(this[y-1][x+1])
  125.         }
  126.         // Two octopuses left and right
  127.         if (x != 0) list.add(this[y][x-1])
  128.         if (x != this[0].size-1) list.add(this[y][x+1])
  129.         // Three octopuses below
  130.         if (y != this.size-1) {
  131.             if (x != 0) list.add(this[y+1][x-1])
  132.             list.add(this[y+1][x])
  133.             if (x != this[0].size-1) list.add(this[y+1][x+1])
  134.         }
  135.         return list
  136.     }
  137.  
  138.     private fun List<Octopus>.step(): Int {
  139.         this.forEach { it.increaseEnergy() }
  140.         return this.sumBy { it.flash() }
  141.     }
  142.  
  143.     private val smallExampleCave = """11111
  144. 19991
  145. 19191
  146. 19991
  147. 11111""".split("\n").map { it.map { it.toString().toInt() }.map { Octopus(it) } }.withSurrounding()
  148.  
  149.     private val largeExampleCave = """5483143223
  150. 2745854711
  151. 5264556173
  152. 6141336146
  153. 6357385478
  154. 4167524645
  155. 2176841721
  156. 6882881134
  157. 4846848554
  158. 5283751526""".split("\n").map { it.map { it.toString().toInt() }.map { Octopus(it) } }.withSurrounding()
  159.  
  160.     private val puzzleCave = """5723573158
  161. 3154748563
  162. 4783514878
  163. 3848142375
  164. 3637724151
  165. 8583172484
  166. 7747444184
  167. 1613367882
  168. 6228614227
  169. 4732225334""".split("\n").map { it.map { it.toString().toInt() }.map { Octopus(it) } }.withSurrounding()
  170. }
  171.  
Advertisement
Add Comment
Please, Sign In to add comment