Created
March 4, 2021 06:59
-
-
Save theevo/ed6ed8124e50b65f3963324148038414 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
/* | |
* Complete the 'cardPackets' function below. | |
* | |
* The function is expected to return an INTEGER. | |
* The function accepts INTEGER_ARRAY cardTypes as parameter. | |
*/ | |
func cardPackets(cardTypes: [Int]) -> Int { | |
// Write your code here | |
// 2 packets | |
var cardMinimums: [Int: Int] = [:] | |
for i in 2...5 { | |
let minimum = cardsNeeded(packets: i, cardTypes: cardTypes) | |
// print("@\(i) packets. total = \(minimum)") | |
cardMinimums[i] = minimum | |
} | |
let cardMinimum = cardMinimums.min { a, b in a.value < b.value } | |
return cardMinimum?.value ?? 0 | |
} | |
func nextMod(_ a: Int, _ b: Int) -> Int { | |
let nextQuotient: Int = (a / b) + 1 | |
let nextMod = (b * nextQuotient) - a | |
// print("\(a) %^ \(b) = \(nextMod)") | |
return nextMod | |
} | |
func cardsNeeded(packets: Int, cardTypes: [Int]) -> Int { | |
var cardCount = 0 | |
for type in cardTypes { | |
if type % packets != 0 { | |
let remainder = nextMod(type, packets) | |
cardCount += remainder | |
} | |
} | |
return cardCount | |
} | |
func generateRandomArray() -> [Int] { | |
var arr: [Int] = [] | |
for _ in 0...9 { | |
arr.append(Int.random(in: 1...9)) | |
} | |
// print("random array =", arr) | |
return arr | |
} | |
//let stdout = ProcessInfo.processInfo.environment["OUTPUT_PATH"]! | |
let cards = [3,8,7,6,4] | |
cardPackets(cardTypes: cards) // 2 | |
let cards2 = generateRandomArray() | |
cardPackets(cardTypes: cards2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment