From 5b17bdf1cfaca52a1a963705e4a1b3de2d24d014 Mon Sep 17 00:00:00 2001 From: roberts1985 Date: Wed, 9 Nov 2022 20:17:38 -0600 Subject: [PATCH 1/3] =?UTF-8?q?Soluci=C3=B3n=20reto=2045=20swift?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .DS_Store | Bin 0 -> 6148 bytes contenedor.playground/Contents.swift | 35 ++++++++++++++++++++ contenedor.playground/contents.xcplayground | 4 +++ 3 files changed, 39 insertions(+) create mode 100644 .DS_Store create mode 100644 contenedor.playground/Contents.swift create mode 100644 contenedor.playground/contents.xcplayground diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..3ca1056c74f0e9427b863dbad513f28bf4d2ec18 GIT binary patch literal 6148 zcmeHK!A=4(5PbzqKrdWOyl9$u@J5vQ0Sm%`L_NsCs{$(;S89yJaND~d<0tByw$ZRi z!bymk*-T$Mo$0i%o9z~WX-`KdKpjApDp;zq_(h~&bU{kdvPCq0j1&{37{TFX!CMWR z$bjtKE{5E(30}E(Kl|6fGcw#_hB0^3VamQyFLT2|<_3)7E8|~)?q?L*wsDRg&d|jr z`YaoGD#oBizeWgQ4XELYo-K56u_WT75OKt@9p^UXydU7ohBLFAW6Bjc)()wse5_xO zrS}{C?%2D+n#_A4+X1dQPKrlnoN;aUaaZGKlH1>XBY&KPt0cAiLSOWtl z|`0NQ{k`M9c%Shhjn~F@&0U z#V{e9b{qYYh)!-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 From bd2ad1f8150abb7bd56a10dabbe0783bf7b9ddfb Mon Sep 17 00:00:00 2001 From: roberts1985 Date: Thu, 17 Nov 2022 15:27:31 -0600 Subject: [PATCH 2/3] =?UTF-8?q?Soluci=C3=B3n=20reto=2046=20Swift?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robot.playground/Contents.swift | 64 ++++++++++++++++++++++++++ robot.playground/contents.xcplayground | 4 ++ 2 files changed, 68 insertions(+) create mode 100644 robot.playground/Contents.swift create mode 100644 robot.playground/contents.xcplayground 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 From b4ee18dd9343f8dcfae3a85456810718d8818f62 Mon Sep 17 00:00:00 2001 From: roberts1985 Date: Thu, 17 Nov 2022 15:31:26 -0600 Subject: [PATCH 3/3] Update .DS_Store --- .DS_Store | Bin 6148 -> 6148 bytes 1 file changed, 0 insertions(+), 0 deletions(-) diff --git a/.DS_Store b/.DS_Store index 3ca1056c74f0e9427b863dbad513f28bf4d2ec18..d0383c67d74761d7ae4f02fb3248b0e3fad4220b 100644 GIT binary patch delta 27 fcmZoMXffE}!NkY-i-Cc`hTAk$Vh&