I forgot to copy the problem, damn
Last active
April 20, 2024 08:20
-
-
Save NanamiNakano/e19e238ae10f3ea0407657e7eccd6c1b to your computer and use it in GitHub Desktop.
ACSL 2023-2024 Intermediate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun calculateDiscardPileSum(initialRows: String, tilesInput: String): Int { | |
val rows = initialRows.map { it.toString().toInt() }.toMutableList() | |
val tiles = tilesInput.split(" ").map { tile -> | |
Pair(tile[0].toString().toInt(), tile[1].toString().toInt()) | |
} | |
val discardPile = mutableListOf<Int>() | |
var lastIndex = -1 // Start with -1 to indicate no tile has been placed yet | |
for (tile in tiles) { | |
val (firstDigit, secondDigit) = tile | |
var placed = false | |
// Attempt to place the tile without reversing | |
for (i in 0 until 4) { | |
if (lastIndex != -1 && i == lastIndex && firstDigit == secondDigit) { | |
// Skip if the last tile was a double and placed on this row | |
continue | |
} | |
val currentIndex = (lastIndex + 1 + i) % 4 | |
when (rows[currentIndex]) { | |
firstDigit -> { | |
rows[currentIndex] = secondDigit | |
lastIndex = currentIndex | |
placed = true | |
break | |
} | |
secondDigit -> { | |
rows[currentIndex] = firstDigit | |
lastIndex = currentIndex | |
placed = true | |
break | |
} | |
} | |
} | |
// If not placed, attempt to place the tile with reversing | |
if (!placed) { | |
for (i in 0 until 4) { | |
if (lastIndex != -1 && i == lastIndex && firstDigit == secondDigit) { | |
// Skip if the last tile was a double and placed on this row | |
continue | |
} | |
val currentIndex = (lastIndex + 1 + i) % 4 | |
if (rows[currentIndex] == secondDigit) { | |
rows[currentIndex] = firstDigit | |
lastIndex = currentIndex | |
placed = true | |
break | |
} | |
} | |
} | |
// If the tile still wasn't placed, add it to the discard pile | |
if (!placed) { | |
discardPile.add(firstDigit) | |
discardPile.add(secondDigit) | |
} | |
} | |
// Calculate the sum of the discard pile | |
return discardPile.sum() | |
} | |
// Test the function with some input | |
fun main() { | |
val initialRows = readLine()!! | |
val tiles = readLine()!! | |
// Call the function and print the result | |
val result = calculateDiscardPileSum(initialRows, tiles) | |
println(result) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main() { | |
val symbols = listOf("!", "?", ".", ",", ":", ";", "(", ")", "[", "]", "{", "}", "<", ">", "'", "\"", "-") | |
val regex = Regex("\\b\\w+\\b") | |
val dic = readln().split("""(?<=[.!?])\s+""".toRegex()) | |
.map { s -> regex.findAll(s).map { it.value }.toList().filter { it !in symbols }.map { it.toList() } } | |
val encrypted = readln().split(" ").map { s -> s.split(".").map { it.toInt() } } | |
val result = encrypted.joinToString("") { | |
val char = (dic.getOrNull(it[0] - 1)?.getOrNull(it[1] - 1)?.getOrNull(it[2] - 1) ?: " ").toString() | |
if (char in symbols) { | |
" " | |
} else { | |
char | |
} | |
} | |
println(result) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main() { | |
val input = readln().split(" ").map(String::toInt).chunked(5).map { it.toMutableList() }.toMutableList() | |
val startBlock = fineStart(input) | |
val result = mutableListOf(startBlock) | |
findPath(input, startBlock, result) | |
val sum = result.sumOf { input[it.first][it.second] } | |
result.forEach { | |
input[it.first][it.second] = 0 | |
} | |
input[result.last().first][result.last().second] = findMinBlock(sum) | |
val kept = input.flatten().distinct().sortedDescending().take(8) | |
for (i in input.indices) { | |
for (j in input[i].indices) { | |
if (input[i][j] !in kept) { | |
input[i][j] = 0 | |
} | |
} | |
} | |
moveNumbersDown(input) | |
fillGridWithNumbers(input, kept) | |
println(input.flatten().joinToString(" ")) | |
} | |
fun fineStart(input: List<List<Int>>): Pair<Int, Int> { | |
val startLine = input[0] | |
startLine.forEachIndexed { index, num -> | |
if (num == startLine[index + 1]) { | |
return Pair(0, index) | |
} | |
if (index > 0) { | |
if (num == input[1][index - 1]) { | |
return Pair(0, index) | |
} | |
} | |
if (num == input[1][index]) { | |
return Pair(0, index) | |
} | |
if (num == input[1][index + 1]) { | |
return Pair(0, index) | |
} | |
} | |
return Pair(-1, -1) | |
} | |
fun findPath(input: List<List<Int>>, currentBlock: Pair<Int, Int>, result: MutableList<Pair<Int, Int>>) { | |
val possible = mutableListOf<Pair<Int, Int>>() | |
val inputIndex = currentBlock.first | |
val currentIndex = currentBlock.second | |
val inputLine = input[inputIndex] | |
val current = inputLine[currentIndex] | |
if (currentIndex > 0) { | |
if (inputLine[currentIndex - 1] == current || inputLine[currentIndex - 1] == current * 2) { | |
val found = Pair(inputIndex, currentIndex - 1) | |
if (!result.contains(found)) { | |
possible.add(found) | |
} | |
} | |
} | |
if (currentIndex < 4) { | |
if (inputLine[currentIndex + 1] == current || inputLine[currentIndex + 1] == current * 2) { | |
val found = Pair(inputIndex, currentIndex + 1) | |
if (!result.contains(found)) { | |
possible.add(found) | |
} | |
} | |
} | |
if (inputIndex < input.size - 1) { | |
val nextLine = input[inputIndex + 1] | |
if (currentIndex > 0) { | |
if (nextLine[currentIndex - 1] == current || nextLine[currentIndex - 1] == current * 2) { | |
val found = Pair(inputIndex + 1, currentIndex - 1) | |
if (!result.contains(found)) { | |
possible.add(found) | |
} | |
} | |
} | |
if (nextLine[currentIndex] == current || nextLine[currentIndex] == current * 2) { | |
val found = Pair(inputIndex + 1, currentIndex) | |
if (!result.contains(found)) { | |
possible.add(found) | |
} | |
} | |
if (currentIndex < 4) { | |
if (nextLine[currentIndex + 1] == current || nextLine[currentIndex + 1] == current * 2) { | |
val found = Pair(inputIndex + 1, currentIndex + 1) | |
if (!result.contains(found)) { | |
possible.add(found) | |
} | |
} | |
} | |
} | |
val possibleInt = possible.minOfOrNull { input[it.first][it.second] } ?: return | |
val nextIndex = possible.map { input[it.first][it.second] }.indexOf(possibleInt) | |
result.add(possible[nextIndex]) | |
findPath(input, possible[nextIndex], result) | |
} | |
fun findMinBlock(num: Int, start: Int = 1): Int { | |
return if (start < num) { | |
findMinBlock(num, start * 2) | |
} else { | |
start | |
} | |
} | |
fun moveNumbersDown(grid: MutableList<MutableList<Int>>) { | |
val numRows = grid.size | |
val numCols = grid[0].size | |
for (col in 0 until numCols) { | |
var nonZeroIndex = numRows - 1 | |
for (row in numRows - 1 downTo 0) { | |
if (grid[row][col] != 0) { | |
grid[nonZeroIndex][col] = grid[row][col] | |
if (nonZeroIndex != row) { | |
grid[row][col] = 0 | |
} | |
nonZeroIndex-- | |
} | |
} | |
} | |
} | |
fun fillGridWithNumbers(grid: MutableList<MutableList<Int>>, numbers: List<Int>) { | |
var index = 0 | |
for (row in 0 until grid.size) { | |
for (col in 0 until grid[row].size) { | |
if (grid[row][col] == 0) { | |
grid[row][col] = numbers[index] | |
index = (index + 1) % numbers.size | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment