From 6b94a49d047a4f34f7a0b3bbb1f418bb5f552c92 Mon Sep 17 00:00:00 2001 From: roberts1985 Date: Thu, 17 Nov 2022 15:46:21 -0600 Subject: [PATCH] =?UTF-8?q?Soluci=C3=B3n=20reto=2046=20en=20Swift?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .DS_Store | Bin 0 -> 6148 bytes robot.playground/Contents.swift | 64 +++++++++++++++++++++++++ robot.playground/contents.xcplayground | 4 ++ 3 files changed, 68 insertions(+) create mode 100644 .DS_Store create mode 100644 robot.playground/Contents.swift create mode 100644 robot.playground/contents.xcplayground diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..d0383c67d74761d7ae4f02fb3248b0e3fad4220b GIT binary patch literal 6148 zcmeHK!A=4(5PbzqKrdWOyl9$u@J5vI0Sm%`L_EmBs{$(;SJoJb;kI`_#{cu8Z`wv> zk%W^FGqah#?sTTpc_mv4z_g~L6QBy9Ocg9t*!&_gF1jFvVA&!XH%E*KVvLaDX~A0! zo5+Cd-7bdQu?e2Jci;P0!y^)0V}>zzGsTo+qi&K82T3|$6n`@2o!Izr;)>hG8M-({ z2j}Rst>GabgC;#|KVmAlpl1_p^p-@tDSL<11|RF{ zvGjhS-wj7ssp&PB?EsgYC&oQ9&RE+mIZ5~kS7Wwy98>En&Ap$So3AR$fHI&AtbqaJ zbC!43kXutK1IoZ=Ga%mwOBIX)W**(AgN?fa5KA;WVPEbN5@Q2K0W*&rp%@=Z^r0qP zF^mtV-3GrXVCK<>!-UI+37Ji}p%|B)^IPW*6M58D8Bhl187TVKs+|9w<@f*jAbnE? zl!1T6fGM^6?G{T4XX`?7a@Jbv6;(w1GLK&&?6{+tzH$`rQJv6kr9+GYW*%vw=pO+` LgEq>*pEB?X0MKQ? literal 0 HcmV?d00001 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