<commit_before>;; Game of Life again.
;; Represent a grid as a set of populated locations.
;; A location is an `(,x ,y) coordinate pair.

(import (use "lib/sturm")
  cbreak-mode)
(import (use "lib/ansi-term")
  home clear-screen cursor-show cursor-hide)

(to (main args)
  (let n-steps (match args
                 (`(,_) 20)
                 (`(,_ ,n-str) (number<-string n-str))
                 (`(,prog ,@_) (error ("Usage: ~d [#steps]" .format prog)))))
  (for cbreak-mode ()
    (display cursor-hide)
    (run r-pentomino n-steps)
    (display cursor-show)))

(to (run grid n-steps)
  (for foldl ((grid grid) (_ (1 .to n-steps)))
    (display clear-screen)
    (display home)
    (show grid)
    (update grid)))

(to (update grid)
  (let active (bag<- (gather neighbors grid.keys)))
  ('.range (for filter ((`(,pos ,n-live) active.items))
             (match n-live
               (3 pos)
               (2 (and (grid .maps? pos) pos))
               (_ #no)))))

(to (neighbors `(,x ,y))
  (for gather ((dx '(-1 0 1)))
    (for filter ((dy '(-1 0 1)))
      (and (not= `(,dx ,dy) '(0 0))
           `(,(+ x dx) ,(+ y dy))))))

(to (show grid)
  (let `((,x-lo ,x-hi) (,y-lo ,y-hi))
    (each bounds<- (transpose grid.keys)))
  (for each! ((y (y-lo .to y-hi)))
    (for each! ((x (x-lo .to x-hi)))
      (display (if (grid .maps? `(,x ,y)) "O " "  ")))
    (newline)))<commit_msg>tweak<commit_after>;; Game of Life again.
;; Represent a grid as a set of populated locations.
;; A location is an `(,x ,y) coordinate pair.

(import (use "lib/sturm")
  cbreak-mode)
(import (use "lib/ansi-term")
  clear-screen cursor-show cursor-hide)

(to (main args)
  (let n-steps (match args
                 (`(,_) 20)
                 (`(,_ ,n-str) (number<-string n-str))
                 (`(,prog ,@_) (error ("Usage: ~d [#steps]" .format prog)))))
  (for cbreak-mode ()
    (display cursor-hide)
    (run r-pentomino n-steps)
    (display cursor-show)))

(to (run grid n-steps)
  (for foldl ((grid grid) (_ (1 .to n-steps)))
    (display clear-screen)
    (show grid)
    (update grid)))

(to (update grid)
  (let active (bag<- (gather neighbors grid.keys)))
  ('.range (for filter ((`(,pos ,n-live) active.items))
             (match n-live
               (3 pos)
               (2 (and (grid .maps? pos) pos))
               (_ #no)))))

(to (neighbors `(,x ,y))
  (for gather ((dx '(-1 0 1)))
    (for filter ((dy '(-1 0 1)))
      (and (not= `(,dx ,dy) '(0 0))
           `(,(+ x dx) ,(+ y dy))))))

(to (show grid)
  (let `((,x-lo ,x-hi) (,y-lo ,y-hi))
    (each bounds<- (transpose grid.keys)))
  (for each! ((y (y-lo .to y-hi)))
    (for each! ((x (x-lo .to x-hi)))
      (display (if (grid .maps? `(,x ,y)) "O " "  ")))
    (newline)))
{'fraction_non_alphanumeric': 0.21475529583637692, 'fraction_numerical': 0.009495982468955442, 'mean_word_length': 2.2529691211401426, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '178757', 'n_tokens_mistral': 1069, 'n_tokens_neox': 1028, 'n_words': 322}