-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path방문 길이.swift
More file actions
78 lines (69 loc) · 2.59 KB
/
방문 길이.swift
File metadata and controls
78 lines (69 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//
// 방문 길이.swift
//
//
// Created by chihoooon on 2022/01/23.
//
import Foundation
func solution(_ dirs: String) -> Int {
var coordinate = [[Int]](repeating: [Int](repeating: 0, count: 6), count: 6)
let direction = Array(dirs)
var currentPos = [0, 0]
var answer = Set<String>()
coordinate[0][0] = 1
for dir in direction {
if dir == "U" {
if currentPos[1] + 1 <= 5 {
answer.insert("\(currentPos[0])" +
"\(currentPos[1])" +
"\(currentPos[0])" +
"\(currentPos[1] + 1)")
answer.insert("\(currentPos[0])" +
"\(currentPos[1] + 1)" +
"\(currentPos[0])" +
"\(currentPos[1])")
currentPos[1] += 1
}
}
else if dir == "D" {
if currentPos[1] - 1 >= -5 {
answer.insert("\(currentPos[0])" +
"\(currentPos[1])" +
"\(currentPos[0])" +
"\(currentPos[1] - 1)")
answer.insert("\(currentPos[0])" +
"\(currentPos[1] - 1)" +
"\(currentPos[0])" +
"\(currentPos[1])")
currentPos[1] -= 1
}
}
else if dir == "R" {
if currentPos[0] + 1 <= 5 {
answer.insert("\(currentPos[0])" +
"\(currentPos[1])" +
"\(currentPos[0] + 1)" +
"\(currentPos[1])")
answer.insert("\(currentPos[0] + 1)" +
"\(currentPos[1])" +
"\(currentPos[0])" +
"\(currentPos[1])")
currentPos[0] += 1
}
}
else {
if currentPos[0] - 1 >= -5 {
answer.insert("\(currentPos[0])" +
"\(currentPos[1])" +
"\(currentPos[0] - 1)" +
"\(currentPos[1])")
answer.insert("\(currentPos[0] - 1)" +
"\(currentPos[1])" +
"\(currentPos[0])" +
"\(currentPos[1])")
currentPos[0] -= 1
}
}
}
return answer.count / 2
}