|
1 | 1 | private const val DAY = 11 |
2 | | - |
3 | 2 | fun main() { |
4 | | - fun part1(input: List<String>): Int { |
5 | | - return -1 |
| 3 | + fun part1(octopuses: Array<IntArray>): Int { |
| 4 | + var flashCount = 0 |
| 5 | + repeat(100) { |
| 6 | + // increase energy level of all by one |
| 7 | + for (x in octopuses.indices) { |
| 8 | + for (y in octopuses[0].indices) { |
| 9 | + octopuses[x][y]++ |
| 10 | + } |
| 11 | + } |
| 12 | + // flash all octopuses with energy > 9 plus adjacent ones |
| 13 | + val flashedOctopuses: Array<BooleanArray> = Array(10) { BooleanArray(10) } |
| 14 | + for (x in octopuses.indices) { |
| 15 | + for (y in octopuses[0].indices) { |
| 16 | + if (octopuses[x][y] > 9 && !flashedOctopuses[x][y]) { |
| 17 | + //octopuses[x][y]++ |
| 18 | + makeOctopusFlash(octopuses, x, y, flashedOctopuses) |
| 19 | + } |
| 20 | + } |
| 21 | + } |
| 22 | + // reset energy level |
| 23 | + for (x in octopuses.indices) { |
| 24 | + for (y in octopuses[0].indices) { |
| 25 | + if (octopuses[x][y] > 9) { |
| 26 | + octopuses[x][y] = 0 |
| 27 | + flashCount++ |
| 28 | + } |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | + return flashCount |
6 | 33 | } |
7 | 34 |
|
8 | | - fun part2(input: List<String>): Int { |
| 35 | + fun part2(octopuses: Array<IntArray>): Int { |
9 | 36 | return -1 |
10 | 37 | } |
11 | 38 |
|
12 | 39 | // test if implementation meets criteria from the description, like: |
13 | | - val testInput = readInput(day = DAY, useTestInput = true) |
| 40 | + val testInput = readInput(day = DAY, useTestInput = true).toIntArray() |
14 | 41 | check(part1(testInput) == 1656) |
15 | 42 | //check(part2(testInput) == 288957) |
16 | 43 |
|
17 | | - val input = readInput(day = DAY) |
| 44 | + val input = readInput(day = DAY).toIntArray() |
18 | 45 | println(part1(input)) |
19 | 46 | println(part2(input)) |
20 | 47 | } |
| 48 | + |
| 49 | +private fun makeOctopusFlash(octopuses: Array<IntArray>, x: Int, y: Int, flashedOctopuses: Array<BooleanArray>) { |
| 50 | + flashedOctopuses[x][y] = true |
| 51 | + |
| 52 | + var xIndex = x-1 |
| 53 | + var yIndex = y |
| 54 | + if (x > 0) { |
| 55 | + octopuses[xIndex][yIndex]++ |
| 56 | + if (octopuses[xIndex][yIndex] > 9 && !flashedOctopuses[xIndex][yIndex]) { |
| 57 | + makeOctopusFlash(octopuses, xIndex, yIndex, flashedOctopuses) |
| 58 | + } |
| 59 | + } |
| 60 | + xIndex = x+1 |
| 61 | + yIndex = y |
| 62 | + if (x < octopuses.size - 1) { |
| 63 | + octopuses[xIndex][yIndex]++ |
| 64 | + if (octopuses[xIndex][yIndex] > 9 && !flashedOctopuses[xIndex][yIndex]) { |
| 65 | + makeOctopusFlash(octopuses, xIndex, yIndex, flashedOctopuses) |
| 66 | + } |
| 67 | + } |
| 68 | + xIndex = x |
| 69 | + yIndex = y-1 |
| 70 | + if (y > 0) { |
| 71 | + octopuses[xIndex][yIndex]++ |
| 72 | + if (octopuses[xIndex][yIndex] > 9 && !flashedOctopuses[xIndex][yIndex]) { |
| 73 | + makeOctopusFlash(octopuses, xIndex, yIndex, flashedOctopuses) |
| 74 | + } |
| 75 | + } |
| 76 | + xIndex = x |
| 77 | + yIndex = y+1 |
| 78 | + if (y < octopuses[0].size - 1) { |
| 79 | + octopuses[xIndex][yIndex]++ |
| 80 | + if (octopuses[xIndex][yIndex] > 9 && !flashedOctopuses[xIndex][yIndex]) { |
| 81 | + makeOctopusFlash(octopuses, xIndex, yIndex, flashedOctopuses) |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + xIndex = x-1 |
| 86 | + yIndex = y-1 |
| 87 | + if (x > 0 && y > 0) { |
| 88 | + octopuses[xIndex][yIndex]++ |
| 89 | + if (octopuses[xIndex][yIndex] > 9 && !flashedOctopuses[xIndex][yIndex]) { |
| 90 | + makeOctopusFlash(octopuses, xIndex, yIndex, flashedOctopuses) |
| 91 | + } |
| 92 | + } |
| 93 | + xIndex = x+1 |
| 94 | + yIndex = y+1 |
| 95 | + if (y < octopuses[0].size - 1 && x < octopuses.size - 1) { |
| 96 | + octopuses[xIndex][yIndex]++ |
| 97 | + if (octopuses[xIndex][yIndex] > 9 && !flashedOctopuses[xIndex][yIndex]) { |
| 98 | + makeOctopusFlash(octopuses, xIndex, yIndex, flashedOctopuses) |
| 99 | + } |
| 100 | + } |
| 101 | + xIndex = x+1 |
| 102 | + yIndex = y-1 |
| 103 | + if (x < octopuses.size - 1 && y > 0) { |
| 104 | + octopuses[xIndex][yIndex]++ |
| 105 | + if (octopuses[xIndex][yIndex] > 9 && !flashedOctopuses[xIndex][yIndex]) { |
| 106 | + makeOctopusFlash(octopuses, xIndex, yIndex, flashedOctopuses) |
| 107 | + } |
| 108 | + } |
| 109 | + xIndex = x-1 |
| 110 | + yIndex = y+1 |
| 111 | + if (y < octopuses.size - 1 && x > 0) { |
| 112 | + octopuses[xIndex][yIndex]++ |
| 113 | + if (octopuses[xIndex][yIndex] > 9 && !flashedOctopuses[xIndex][yIndex]) { |
| 114 | + makeOctopusFlash(octopuses, xIndex, yIndex, flashedOctopuses) |
| 115 | + } |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +private fun List<String>.toIntArray(): Array<IntArray> { |
| 120 | + return map { it.toCharArray().map { it.toString().toInt() }.toIntArray() }.toTypedArray() |
| 121 | +} |
0 commit comments