Created
March 25, 2023 20:21
-
-
Save jakobrs/bf9dcadd21174f3b013ab72aa340fd54 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#set page(width: auto, height: auto, margin: 2cm) | |
#{ | |
let width = 10 | |
let height = 10 | |
let grid = ((false,) * width,) * height | |
let render_box(color) = box( | |
outset: -0.1cm, | |
radius: 4pt, | |
width: 1cm, | |
height: 1cm, | |
stroke: black, | |
fill: if color { white } else { black } | |
) | |
let render(grid) = { | |
stack( | |
dir: ttb, | |
..grid.map(row => stack( | |
dir: ltr, | |
..row.map(cell => render_box(cell)) | |
)) | |
) | |
} | |
let step(grid) = { | |
let new_grid = ((false,) * width,) * height | |
for r in range(height) { | |
for c in range(width) { | |
let live = 0 | |
for dr in (-1, 0, 1) { | |
for dc in (-1, 0, 1) { | |
if dr == 0 and dc == 0 { continue } | |
if 0 <= r + dr and r + dr < height and 0 <= c + dc and c + dc < width { | |
if grid.at(r + dr).at(c + dc) { | |
live += 1 | |
} | |
} | |
} | |
} | |
if grid.at(r).at(c) { | |
new_grid.at(r).at(c) = live == 2 or live == 3 | |
} else { | |
new_grid.at(r).at(c) = live == 3 | |
} | |
} | |
} | |
new_grid | |
} | |
for cell in ( | |
(1, 3), | |
(2, 1), | |
(2, 3), | |
(3, 2), | |
(3, 3), | |
) { | |
let r = cell.at(0) | |
let c = cell.at(1) | |
grid.at(r).at(c) = true | |
} | |
render(grid) | |
for i in range(10) { | |
pagebreak() | |
// line(length: 1cm * width) | |
grid = step(grid) | |
render(grid) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment