1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Hello World あたたたた Swiftレギュレーション違反編

1
Last updated at Posted at 2025-12-22

この記事は Hello World あたたたた Advent Calendar 2025 の空いた枠を埋める記事です。

面白そうなのがあったのですが、すでにSwift編がありましたので、レギュレーションを逸脱した記事を投稿しております。

Swiftなのでやっぱり型が欲しいです。
だけをランダムに出力したいのでenumにしました。

enum あた: String, Equatable {
  case , 
}

extension あた: CustomStringConvertible {
  var description: String {
    self.rawValue
  }
}

extension あた: CaseIterable {
  static func random<G: RandomNumberGenerator>(using generator: inout G) -> あた {
    あた.allCases.randomElement(using: &generator)!
  }
  
  static func random() -> あた {
    var g = SystemRandomNumberGenerator()
    return あた.random(using: &g)
  }
}

ランダムに取り出したり、出力のためのデータの生成もenumに閉じ込めました。

のこりは

あとは簡単ですね。

let あたたたた: ArraySlice<あた> = [., ., ., ., .]
var values: [あた] = []

// 末尾があたたたたと同一になるまでループ
while(values.suffix(あたたたた.count) != あたたたた) {
  values.append(あた.random())
  // appendしたのでlastは必ず存在します
  print(values.last!, terminator: "")
}

// 改行
print()
print("お前はもう死んでいる")

感想

僕はこうやって型を利用した方法がわかりやすいんですけど、皆さんはどうなんですかね?

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?