Skip to content

Commit 2e67719

Browse files
committed
Solucion reto mouredev#45 Kotlin
1 parent 945efe2 commit 2e67719

File tree

2 files changed

+138
-1
lines changed

2 files changed

+138
-1
lines changed

app/src/main/java/com/mouredev/weeklychallenge2022/Challenge45.kt

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,140 @@ package com.mouredev.weeklychallenge2022
3131
*
3232
*/
3333

34+
/**
35+
* Emojis para representar el agua y los bloques y el vacio
36+
*/
37+
enum class Emojis(val emoji: String?) {
38+
BLOCK("⏹︎"),
39+
WATER("\uD83D\uDCA7"),
40+
EMPTY(" ")
41+
}
42+
43+
fun main(){
44+
println("`Numero de bloques ${calculateWaterBlocks(arrayOf(3, 1, 6, 3, 0, 4))}")
45+
println("`Numero de bloques ${calculateWaterBlocks(arrayOf(3, 0, 0, 2, 0, 4))}")
46+
println("`Numero de bloques ${calculateWaterBlocks(arrayOf(1,0,3,4,0,0,2,1,0,1,2,1,0,1,0,3,2,1,2,1))}")
47+
}
48+
49+
/**
50+
* Funcion que calcula las unidades de agua que se quedan atrapads entre los bloques
51+
* Se recorre empezando por los extremos del array comparando cada elemento.
52+
* - Si el de la izquierda es mayor y es el mayor encontrado por la derecha , entonces ponemos
53+
* como maximo bloqque de altura ese valor.Si no contamos como bloques de agua , los bloques de altura maxima menos los que tiene
54+
* esa columna ya que son los que quedan atrapados , avanzamos a la siguiente columna y repetimos el proceso
55+
* - Si la derecha es mayor que la izquierda y es el mayor de la izquierda , entonces ponemos
56+
* como maximo bloque de altura por la izquierda ese valor.Si no contamos como bloques de agua los bloques de altura maxima menos
57+
* los que tiene esa columna , avanzamos a la siguiente columna y repetimos el proceso.
58+
* Avanzamos y repetimos hasta que la izquierda y la derecha se crucen.
59+
*
60+
* @param blocks Array de bloques de altura
61+
* @returns Numero de bloques de agua que quedan atrapados
62+
*/
63+
64+
fun calculateWaterBlocks(blocks: Array<Int>): Int {
65+
val matrix = Array(blocks.size) { emptyArray<String?>() }
66+
var water = 0
67+
var left = 0
68+
var right = blocks.size-1
69+
70+
var maxLeft = 0
71+
var maxRight = 0
72+
var maxTotal = 0
73+
74+
while (left<right){
75+
76+
if(blocks[left]<blocks[right]){
77+
if(blocks[left]>maxLeft){
78+
maxLeft = blocks[left]
79+
}else{
80+
water += maxLeft - blocks[left]
81+
}
82+
matrix[left] = fillRow(maxLeft,blocks[left])
83+
left++
84+
}else{
85+
if(blocks[right]>maxRight){
86+
maxRight = blocks[right]
87+
}else{
88+
water += maxRight - blocks[right]
89+
}
90+
matrix[right] = fillRow(maxRight,blocks[right])
91+
right--
92+
}
93+
94+
if(maxTotal < maxLeft){
95+
maxTotal = maxLeft;
96+
} else if(maxTotal < maxRight){
97+
maxTotal = maxRight;
98+
}
99+
}
100+
101+
matrix[right] = fillRow(blocks[left],blocks[right]);
102+
103+
if(maxTotal < blocks[right]){
104+
maxTotal = blocks[right];
105+
}
106+
107+
108+
drawMatrix(matrix, maxTotal)
109+
return water
110+
}
111+
/**
112+
* Funcion que rellena una fila con los emojis correspondientes de agua 💧 o bloque 🕋
113+
* @param maxValue Valor maximo de altura del bloque para el agua
114+
* @param value Numero de bloques total que hay
115+
*/
116+
private fun fillRow(maxValue:Int,value:Int): Array<String?> {
117+
val row = Array(maxValue) { Emojis.EMPTY.emoji }
118+
for (i in 0 until value){
119+
row[i] = Emojis.BLOCK.emoji
120+
}
121+
for(i in 0 until maxValue-value){
122+
row[i] = Emojis.WATER.emoji
123+
}
124+
return row
125+
}
126+
127+
/**
128+
* Funcion que rellena la matriz de bloques y agua relleando las columnas que no tienen valor
129+
* para poder pintarla correctamente
130+
* @param matrix Matriz de bloques y agua
131+
* @param max Total de bloques de altura maxima
132+
*/
133+
private fun calculateMatrix(matrix: Array<Array<String?>>,max:Int): Array<Array<String>> {
134+
val newMatrix = Array(max) { emptyArray<String>() }
135+
136+
for(i in 0 until max){
137+
newMatrix[i] = Array(matrix.size) { Emojis.EMPTY.emoji!! }
138+
139+
for(j in 0 until matrix.size){
140+
if(j<matrix[j].size){
141+
if(i<matrix[j].size){
142+
matrix[j][i]?.let {
143+
newMatrix[i][j] = it }
144+
}
145+
}
146+
147+
}
148+
}
149+
return newMatrix
150+
151+
}
152+
/**
153+
* Funcion que pinta la matriz de bloques y agua ya rellenada por consola
154+
* @param matrix Matriz de bloques y agua
155+
* @param maxTotal Total de bloques de altura maxima
156+
*/
157+
private fun drawMatrix(matrix: Array<Array<String?>>,maxTotal:Int) {
158+
159+
val array = calculateMatrix(matrix,maxTotal)
160+
array.reverse()
161+
array.map {
162+
var row=""
163+
it.map {
164+
row += it
165+
}
166+
println(row)
167+
}
168+
169+
170+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#Tue Dec 21 19:27:36 CET 2021
22
distributionBase=GRADLE_USER_HOME
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip
44
distributionPath=wrapper/dists
55
zipStorePath=wrapper/dists
66
zipStoreBase=GRADLE_USER_HOME

0 commit comments

Comments
 (0)