<commit_before>class GenerateNewPuzzle
  def initialize(game:)
    @game = game
  end

  def call
    grid = Grid.new(size: @game.board_size)

    grid_array = grid.randomly_populate!
    create_clues!(grid_array)

    print grid.display_string(@game)
  end

  private

  def create_clues!(grid)
    for column in board_range
      clue = @game.clues.new(position: column, orientation: :column)

      run = 0
      values = []
      for row in board_range
        if grid[row][column]
          run += 1
        elsif run > 0
          values << run
          run = 0
        end
      end

      if run > 0
        values << run
      end

      clue.values = values
      clue.save!
    end

    for row in board_range
      clue = @game.clues.new(position: row, orientation: :row)

      run = 0
      values = []
      for column in board_range
        if grid[row][column]
          run += 1
        elsif run > 0
          values << run
          run = 0
        end
      end

      if run > 0
        values << run
      end

      clue.values = values
      clue.save!
    end
  end<commit_msg>Refactor generate new puzzle ruthlessly<commit_after>class GenerateNewPuzzle
  def initialize(game:)
    @game = game
  end

  def call
    grid = Grid.new(size: @game.board_size)

    grid_array = grid.randomly_populate!

    create_clues!(grid: grid_array, orientation: :row)
    create_clues!(grid: grid_array.transpose, orientation: :column)

    print grid.display_string(@game)
  end

  private

  def create_clues!(grid:, orientation:)
    grid.each_with_index do |line, index|
      clue = @game.clues.new(position: index, orientation: orientation)

      clue.values = calculate_values(line)
      clue.save!
    end
  end

  def calculate_values(line)
    values = []

    run = 0
    line.each do |tile|
      if tile
        run += 1
      elsif run > 0
        values << run
        run = 0
      end
    end

    values << run if run > 0

    values
  end
{'fraction_non_alphanumeric': 0.08616320324379118, 'fraction_numerical': 0.007602635580334516, 'mean_word_length': 1.9244444444444444, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 9, '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': '7040124', 'n_tokens_mistral': 651, 'n_tokens_neox': 615, 'n_words': 174}