-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.swift
45 lines (40 loc) · 1.09 KB
/
main.swift
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
//
// main.swift
// SwiftAlgorithm
//
// Created by Lee Myeonghwan on 2023/02/22.
//
import Foundation
struct Point: Hashable {
let y: Int
let x: Int
}
struct Route: Hashable {
let from: Point
let to: Point
}
func solution(_ arrows:[Int]) -> Int {
var answer = 0
// y, x 이동 좌표
let move = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]
var isVisited: [Point : Bool] = [:]
var isVisitedDirection: [Route: Bool] = [:]
var now = Point(y: 0, x: 0)
isVisited[now] = true
arrows.forEach { arrow in
for _ in 0..<2 {
let next = Point(y: now.y + move[arrow].0, x: now.x + move[arrow].1)
if isVisited[next] == true {
if isVisitedDirection[Route(from: now, to: next)] == nil {
answer += 1
}
} else {
isVisited[next] = true
}
isVisitedDirection[Route(from: now, to: next)] = true
isVisitedDirection[Route(from: next, to: now)] = true
now = next
}
}
return answer
}