<commit_before>(ns icfp2015.gameplay
  (:require [icfp2015.board :refer [make-board transition-board valid-position?]]))

(defn handle-unit
  [ai initial-board unit]
  (let [f (fn [[board commands]]
            (let [new-command (ai board)
                  new-board (transition-board board new-command)]
              [new-board (conj commands new-command)]))
        has-current-unit (fn [[{:keys [current-unit]} _]] current-unit)
        while-unit-active (fn [coll]
                            (let [[left right] (split-with has-current-unit coll)]
                              (conj (vec left) (first right))))
        board-with-spawn (assoc initial-board :current-unit unit)]
    (if (valid-position? board-with-spawn)
      (-> (iterate f [board-with-spawn '()])
          (rest)
          (while-unit-active)
          (last))
      [initial-board nil])))

(defn get-commands
  "Yields a seq of commands given a game and an AI"
  [ai {:keys [width height filled units]}]
  (loop [board (make-board width height filled)
         commands '() ; TODO: use vector
         remaining-units units]
    (if (empty? remaining-units)
      commands
      (let [current-unit (first remaining-units)
            [new-board addtl-commands] (handle-unit ai board current-unit)]
        (if addtl-commands
          (recur new-board (concat commands addtl-commands) (rest remaining-units))
          commands)))))<commit_msg>cjl - Debugging info<commit_after>(ns icfp2015.gameplay
  (:require [icfp2015.board :refer [board->str make-board transition-board valid-position?]]))

(def DEBUG false)

(defn handle-unit
  [ai initial-board unit]
  (let [f (fn [[board commands]]
            (if DEBUG
              (do
                (println (board->str board))
                (println)
                (Thread/sleep 100)))
            (let [new-command (ai board)
                  new-board (transition-board board new-command)]
              [new-board (conj commands new-command)]))
        has-current-unit (fn [[{:keys [current-unit]} _]] current-unit)
        while-unit-active (fn [coll]
                            (let [[left right] (split-with has-current-unit coll)]
                              (conj (vec left) (first right))))
        board-with-spawn (assoc initial-board :current-unit unit)]
    (if (valid-position? board-with-spawn)
      (-> (iterate f [board-with-spawn '()])
          (rest)
          (while-unit-active)
          (last))
      [initial-board nil])))

(defn get-commands
  "Yields a seq of commands given a game and an AI"
  [ai {:keys [width height filled units]}]
  (loop [board (make-board width height filled)
         commands '() ; TODO: use vector
         remaining-units units]
    (if DEBUG
      (do
        (println (board->str board))
        (println)))
    (if (empty? remaining-units)
      commands
      (let [current-unit (first remaining-units)
            [new-board addtl-commands] (handle-unit ai board current-unit)]
        (if addtl-commands
          (recur new-board (concat commands addtl-commands) (rest remaining-units))
          commands)))))
{'fraction_non_alphanumeric': 0.13183279742765272, 'fraction_numerical': 0.006109324758842444, 'mean_word_length': 2.1234939759036147, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, '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': '3796846', 'n_tokens_mistral': 927, 'n_tokens_neox': 903, 'n_words': 261}