diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..d0383c6 Binary files /dev/null and b/.DS_Store differ diff --git a/contenedor.playground/Contents.swift b/contenedor.playground/Contents.swift new file mode 100644 index 0000000..fc87b60 --- /dev/null +++ b/contenedor.playground/Contents.swift @@ -0,0 +1,35 @@ +import Foundation + +func contenedorAgua(arreglo:[Int])->Int{ + var inicio = 0 + var fin = arreglo.count - 1 + var suma = 0 + var dif: Int + + while inicio != fin { + let punteroMenor = arreglo[inicio] < arreglo[fin] ? inicio : fin + if punteroMenor == fin { + fin -= 1 + dif = arreglo[punteroMenor] - arreglo[fin] + while dif >= 0 { + suma += dif + fin -= 1 + dif = arreglo[punteroMenor] - arreglo[fin] + } + } else { + inicio += 1 + dif = arreglo[punteroMenor] - arreglo[inicio] + while dif >= 0 { + suma += dif + inicio += 1 + dif = arreglo[punteroMenor] - arreglo[inicio] + } + } + } + + return suma +} + +print(contenedorAgua(arreglo: [4, 0, 3, 6, 1, 3])) +print(contenedorAgua(arreglo: [4,2,5,6,3,2,3])) +print(contenedorAgua(arreglo: [4,0,6,2,3])) diff --git a/contenedor.playground/contents.xcplayground b/contenedor.playground/contents.xcplayground new file mode 100644 index 0000000..cf026f2 --- /dev/null +++ b/contenedor.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/robot.playground/Contents.swift b/robot.playground/Contents.swift new file mode 100644 index 0000000..2db6f12 --- /dev/null +++ b/robot.playground/Contents.swift @@ -0,0 +1,64 @@ +import Foundation + +func girar(direccion: String) ->String { + var d = "" + + switch direccion { + case "N": + d = "O" + case "S": + d = "E" + case "E": + d = "N" + case "O": + d = "S" + default: + print("Dirección incorrecta") + } + + return d +} + +func avanzar(x: Int, y: Int, dato: Int, direccion: String) -> [Int] { + var xx = x + var yy = y + switch direccion { + case "N": + yy += dato + case "S": + yy -= dato + case "E": + xx += dato + case "O": + xx -= dato + default: + print("Dirección incorrecta...") + } + + return [xx,yy] +} + +func dondeEsRobot(arreglo:[Int]) -> [Int] { + var d = "E" + var x = 0 + var y = 0 + var coordenadas: [Int] + for dato in arreglo { + if dato < 0 { + d = girar(direccion: d) + d = girar(direccion: d) + d = girar(direccion: d) + } else { + d = girar(direccion: d) + } + //print(d) + coordenadas = avanzar(x: x, y: y, dato: abs(dato), direccion: d) + x = coordenadas[0] + y = coordenadas[1] + } + + return [x,y] +} + +print(dondeEsRobot(arreglo: [10,5,-2])) +print(dondeEsRobot(arreglo: [-2,3,-1,4])) diff --git a/robot.playground/contents.xcplayground b/robot.playground/contents.xcplayground new file mode 100644 index 0000000..cf026f2 --- /dev/null +++ b/robot.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file