-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path주차 요금 계산.swift
More file actions
63 lines (50 loc) · 1.73 KB
/
주차 요금 계산.swift
File metadata and controls
63 lines (50 loc) · 1.73 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
//
// 주차 요금 계산.swift
//
//
// Created by chihoooon on 2022/02/09.
//
import Foundation
func calcTimeToInt(_ timeInfo: String) -> Int {
let time = timeInfo.components(separatedBy: ":")
let h = Int(time[0])!
let m = Int(time[1])!
return h * 60 + m
}
func calcFee(_ fees: [Int], time: Int) -> Int {
let standardTime = fees[0]
let standardFee = fees[1]
let unitTime = fees[2]
let unitFee = fees[3]
if time <= standardTime {
return standardFee
}
return standardFee + Int(ceil((Double(time) - Double(standardTime)) / Double(unitTime))) * unitFee
}
func solution(_ fees:[Int], _ records:[String]) -> [Int] {
var result: [String: Int] = [:]
var totalTimeInfo: [String: [Int]] = [:]
let endOfTheDay = 60 * 23 + 59
records.forEach {
let info = $0.components(separatedBy: " ")
let time = info[0]
let carNumber = info[1]
if totalTimeInfo[carNumber] == nil {
totalTimeInfo[carNumber] = []
result[carNumber] = 0
}
totalTimeInfo[carNumber]!.append(calcTimeToInt(time))
}
for (key, value) in totalTimeInfo {
if value.count % 2 != 0 {
totalTimeInfo[key]!.append(endOfTheDay)
}
}
for info in totalTimeInfo {
let inTime = stride(from: 0, to: info.value.count, by: 2).map { info.value[$0] }.reduce(0) { $0 + $1 }
let outTime = stride(from: 1, to: info.value.count, by: 2).map { info.value[$0] }.reduce(0) { $0 + $1 }
let parkingTime = outTime - inTime
result[info.key] = calcFee(fees, time: parkingTime)
}
return result.sorted { $0.key < $1.key }.map { $0.value }
}