RosettaCodeData/Task/Wireworld/11l/wireworld.11l

59 lines
1.6 KiB
Text
Raw Permalink Normal View History

2023-07-01 11:58:00 -04:00
V allstates = Ht.
V head = allstates[0]
V tail = allstates[1]
V conductor = allstates[2]
V empty = allstates[3]
V w =
|tH.........
. .
...
. .
Ht.. ......
2024-07-13 15:19:22 -07:00
T WW = ([[Char]] world, Int w, Int h)
2023-07-01 11:58:00 -04:00
F readfile(f)
V world = f.map(row -> row.rtrim(Array[Char]("\r\n")))
V height = world.len
V width = max(world.map(row -> row.len))
V nonrow = [ ( * width) ]
V world2 = nonrow [+] world.map(row -> String(row).ljust(@width) ) [+] nonrow
V world3 = world2.map(row -> Array(row))
R WW(world3, width, height)
F newcell(currentworld, x, y)
V istate = currentworld[y][x]
assert(istate C :allstates, Wireworld cell set to unknown value "#.".format(istate))
V ostate = :empty
I istate == :head
ostate = :tail
E I istate == :tail
ostate = :conductor
E I istate == :empty
ostate = :empty
E
V n = sum([(-1, -1), (-1, +0), (-1, +1),
(+0, -1), (+0, +1),
(+1, -1), (+1, +0), (+1, +1)].map((dx, dy) -> Int(@currentworld[@y + dy][@x + dx] == :head)))
ostate = I n C 1..2 {:head} E :conductor
R ostate
F nextgen(ww)
V (world, width, height) = ww
V newworld = copy(world)
L(x) 1 .. width
L(y) 1 .. height
newworld[y][x] = newcell(world, x, y)
R WW(newworld, width, height)
F world2string(ww)
R ww.world[1 .< (len)-1].map(row -> (row[1 .< (len)-1]).join().rtrim(( , "\t", "\r", "\n"))).join("\n")
V ww = readfile(w.split("\n"))
2026-02-01 16:33:20 -08:00
L(gen) 0.<10
2023-07-01 11:58:00 -04:00
print(("\n#3 ".format(gen))(= * (ww.w - 4))"\n")
print(world2string(ww))
ww = nextgen(ww)