summaryrefslogtreecommitdiffstats
path: root/krebs/5pkgs/simple/krebsdance/default.nix
blob: cdfe23ef2e6fc603878ab62d5430b146e0d492e9 (plain)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
{ writers }:
writers.writePython3Bin "krebsdance" { flakeIgnore = [ "E501" ]; } ''
  import argparse
  import random
  import itertools

  claws = [
      dict(
          up="(\\/)",
          down="(/\\)",
          left="(\\\\)",
          right="(//)",
      ),
      dict(
          up="(V)",
          down="(A)",
          left=">)=",
          right="=(<",
      ),
      dict(
          up="(U)",
          down="(n)",
          left=")==",
          right="==(",
      ),
  ]

  eyes = [
      "°",
      "*",
      "^",
      "ö",
      "o",
      "O",
      "X",
      "x",
      "U",
      "u",
  ]

  bodies = [
      dict(
          left="(",
          right=")",
      ),
      dict(
          left="{",
          right="}",
      ),
      dict(
          left="[",
          right="]",
      ),
      dict(
          left="<",
          right=">",
      ),
      dict(
          left="|",
          right="|",
      ),
  ]

  mouths = [
      ",,,,",
      ",mm,",
      "_mm_",
      "-mm-",
      ";;;;",
      ";mm;",
      ":mm:",
      "::::",
      ":ww:",
      ":<>:",
  ]


  def all_krebses():
      for mouth, body, eye, claw in itertools.product(mouths, bodies, eyes, claws):
          yield f'{claw["up"]} {body["left"]}{eye}{mouth}{eye}{body["right"]} {claw["up"]}'


  def escape_graph(text):
      return text.replace("\\", "\\\\")


  def krebs_graph() -> str:
      return "\n".join(itertools.chain(
          ["digraph {"],
          [escape_graph(f'"{krebs}"->"{generate(seed=krebs)}"') for krebs in all_krebses()],
          "}",
      ))


  def generate(*, seed: str, dancing: bool = False) -> str:
      if seed:
          random.seed(seed)
      clawstyle = random.choice(claws)
      body = random.choice(bodies)
      eye = random.choice(eyes)
      mouth = random.choice(mouths)
      if dancing:
          return "\n".join(
              [
                  f'{clawstyle["down"]} {body["left"]}{eye}{mouth}{eye}{body["right"]}{clawstyle["up"]}',
                  f'{clawstyle["left"]}{body["left"]}{eye}{mouth}{eye}{body["right"]} {clawstyle["right"]}',
                  f'{clawstyle["right"]} {body["left"]}{eye}{mouth}{eye}{body["right"]} {clawstyle["left"]}',
                  f'{clawstyle["down"]}{body["left"]}{eye}{mouth}{eye}{body["right"]}{clawstyle["down"]}',
              ]
          )
      else:
          return f'{clawstyle["up"]} {body["left"]}{eye}{mouth}{eye}{body["right"]} {clawstyle["up"]}'


  def fixpoints():
      for krebs in all_krebses():
          if generate(seed=krebs) == krebs:
              yield krebs


  def main():
      parser = argparse.ArgumentParser()

      parser.add_argument(
          "seed",
          nargs="?",
          help="random seed to use for generating the krebs variant",
      )

      parser.add_argument(
          "--dance",
          "-d",
          dest="dance",
          help="if the krebs should dance",
          default=False,
          action="store_true",
      )

      parser.add_argument(
          "--mode",
          "-m",
          dest="mode",
          choices=["graphviz", "plain"],
          default="plain",
      )

      args = parser.parse_args()

      if args.mode == "plain":
          print(generate(seed=args.seed, dancing=args.dance))
      elif args.mode == "graphviz":
          print(krebs_graph())


  if __name__ == "__main__":
      main()
''