Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeltroger committed Dec 12, 2021
1 parent 19427ce commit 3c1d299
Showing 1 changed file with 15 additions and 48 deletions.
63 changes: 15 additions & 48 deletions src/Day11.kt
Original file line number Diff line number Diff line change
Expand Up @@ -73,70 +73,37 @@ private fun increaseEnergyLevelOfAllByOne(octopuses: Array<IntArray>) {
private fun makeOctopusFlash(octopuses: Array<IntArray>, x: Int, y: Int, flashedOctopuses: Array<BooleanArray>) {
flashedOctopuses[x][y] = true

var xIndex = x-1
var yIndex = y
if (x > 0) {
octopuses[xIndex][yIndex]++
if (octopuses[xIndex][yIndex] > 9 && !flashedOctopuses[xIndex][yIndex]) {
makeOctopusFlash(octopuses, xIndex, yIndex, flashedOctopuses)
}
increaseEnergyAndFlashIfThresholdReached(octopuses, x-1, y, flashedOctopuses)
}
xIndex = x+1
yIndex = y
if (x < octopuses.size - 1) {
octopuses[xIndex][yIndex]++
if (octopuses[xIndex][yIndex] > 9 && !flashedOctopuses[xIndex][yIndex]) {
makeOctopusFlash(octopuses, xIndex, yIndex, flashedOctopuses)
}
increaseEnergyAndFlashIfThresholdReached(octopuses, x+1, y, flashedOctopuses)
}
xIndex = x
yIndex = y-1
if (y > 0) {
octopuses[xIndex][yIndex]++
if (octopuses[xIndex][yIndex] > 9 && !flashedOctopuses[xIndex][yIndex]) {
makeOctopusFlash(octopuses, xIndex, yIndex, flashedOctopuses)
}
increaseEnergyAndFlashIfThresholdReached(octopuses, x, y-1, flashedOctopuses)
}
xIndex = x
yIndex = y+1
if (y < octopuses[0].size - 1) {
octopuses[xIndex][yIndex]++
if (octopuses[xIndex][yIndex] > 9 && !flashedOctopuses[xIndex][yIndex]) {
makeOctopusFlash(octopuses, xIndex, yIndex, flashedOctopuses)
}
increaseEnergyAndFlashIfThresholdReached(octopuses, x, y+1, flashedOctopuses)
}

xIndex = x-1
yIndex = y-1
if (x > 0 && y > 0) {
octopuses[xIndex][yIndex]++
if (octopuses[xIndex][yIndex] > 9 && !flashedOctopuses[xIndex][yIndex]) {
makeOctopusFlash(octopuses, xIndex, yIndex, flashedOctopuses)
}
increaseEnergyAndFlashIfThresholdReached(octopuses, x-1, y-1, flashedOctopuses)
}
xIndex = x+1
yIndex = y+1
if (y < octopuses[0].size - 1 && x < octopuses.size - 1) {
octopuses[xIndex][yIndex]++
if (octopuses[xIndex][yIndex] > 9 && !flashedOctopuses[xIndex][yIndex]) {
makeOctopusFlash(octopuses, xIndex, yIndex, flashedOctopuses)
}
increaseEnergyAndFlashIfThresholdReached(octopuses, x+1, y+1, flashedOctopuses)
}
xIndex = x+1
yIndex = y-1
if (x < octopuses.size - 1 && y > 0) {
octopuses[xIndex][yIndex]++
if (octopuses[xIndex][yIndex] > 9 && !flashedOctopuses[xIndex][yIndex]) {
makeOctopusFlash(octopuses, xIndex, yIndex, flashedOctopuses)
}
increaseEnergyAndFlashIfThresholdReached(octopuses, x+1, y-1, flashedOctopuses)
}
xIndex = x-1
yIndex = y+1
if (y < octopuses.size - 1 && x > 0) {
octopuses[xIndex][yIndex]++
if (octopuses[xIndex][yIndex] > 9 && !flashedOctopuses[xIndex][yIndex]) {
makeOctopusFlash(octopuses, xIndex, yIndex, flashedOctopuses)
}
increaseEnergyAndFlashIfThresholdReached(octopuses, x-1, y+1, flashedOctopuses)
}
}

private fun increaseEnergyAndFlashIfThresholdReached(octopuses: Array<IntArray>, xIndex: Int, yIndex: Int, flashedOctopuses: Array<BooleanArray>) {
octopuses[xIndex][yIndex]++
if (octopuses[xIndex][yIndex] > 9 && !flashedOctopuses[xIndex][yIndex]) {
makeOctopusFlash(octopuses, xIndex, yIndex, flashedOctopuses)
}
}

Expand Down

0 comments on commit 3c1d299

Please sign in to comment.