Skip to content

Instantly share code, notes, and snippets.

@ShikiSuen
Last active March 31, 2024 08:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ShikiSuen/8ed15132ec6e940d6c80ac11c771b149 to your computer and use it in GitHub Desktop.
Save ShikiSuen/8ed15132ec6e940d6c80ac11c771b149 to your computer and use it in GitHub Desktop.
// (c) 2024 Shiki Suen. (MIT-License).
// 用於《崩壞:星穹鐵道》2.1 版的調酒活動。
struct 特調 {
let 名稱: String
let 甜度: Int // 甜度
let 柔和度: Int // 柔和度
let 清爽度: Int // 清爽度
public init(_ 名稱: String?, _ 參數: (Int, Int, Int)) {
self.名稱 = 名稱 ?? ""
甜度 = 參數.0
柔和度 = 參數.1
清爽度 = 參數.2
}
func 是否與之參數相等(_ 參照對象: 特調) -> Bool {
甜度 == 參照對象.甜度 && 柔和度 == 參照對象.柔和度 && 清爽度 == 參照對象.清爽度
}
func 是否與之參數相等(_ 參照對象: [特調]) -> Bool {
let summed = 參照對象.參數總結
return 甜度 == summed.0 && 柔和度 == summed.1 && 清爽度 == summed.2
}
}
extension Array where Element == 特調 {
var 參數總結: (Int, Int, Int) {
var (x, y, z) = (0, 0, 0)
self.forEach { 飲品副本 in
x += 飲品副本.甜度
y += 飲品副本.柔和度
z += 飲品副本.清爽度
}
return (x, y, z)
}
}
let 所有原材料: [特調] = [
.init("羊奶", (2, 0, 2)),
.init("博士", (1, 0, -1)),
.init("冰點", (1, 0, -2)),
.init("安神", (-1, 0, -1)),
.init("怪味", (-2, 0, 1)),
.init("激夢", (0, 2, 1)),
.init("糖漿", (0, 1, 2)),
.init("清露", (0, 1, -1)),
.init("夕紅", (0, -1, 1)),
.init("能量", (0, -1, 0)),
.init("豆汁", (0, -2, 2)),
]
func 求窮舉組合<T>(成員: ArraySlice<T>, 單筆結果容量: Int) -> [[T]] {
guard 單筆結果容量 != 0, let 第一筆結果 = 成員.first else { return [[]] }
let 陣列頭 = [第一筆結果]
let 子窮舉組合 = 求窮舉組合(成員: 成員, 單筆結果容量: 單筆結果容量 - 1)
return 子窮舉組合.map { 陣列頭 + $0 } + 求窮舉組合(成員: 成員.dropFirst(), 單筆結果容量: 單筆結果容量)
}
func 求窮舉組合<T>(成員: [T], 單筆結果容量: Int) -> [[T]] {
return 求窮舉組合(成員: ArraySlice(成員), 單筆結果容量: 單筆結果容量)
}
enum 容器尺寸: Int {
case S = 3
case L = 4
case XL = 5
}
// 「期望形態」填寫期望的飲料特徵;「所需原料份量」根據杯子的大小填寫要倒幾次成分飲料。
func 調酒(_ 期望形態: 特調, 杯子大小: 容器尺寸) -> [特調]? {
let 所需原料份量 = 杯子大小.rawValue
let 所有可重複窮舉組合 = 求窮舉組合(成員: 所有原材料, 單筆結果容量: 所需原料份量)
var 結果貯存用陣列 = [[特調]]()
for 組合 in 所有可重複窮舉組合 {
guard 期望形態.是否與之參數相等(組合) else { continue }
結果貯存用陣列.append(組合)
}
return 結果貯存用陣列.filter {
$0.count == 所需原料份量
}.randomElement()
}
// ---------------
// 開始測試。
let 希望調製成的樣子 = 特調(nil, (0, 1, 1))
if let 結果 = 調酒(希望調製成的樣子, 杯子大小: .L) {
結果.forEach { component in
print(component)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment