<commit_before>package ru.org.codingteam.icfpc

import ru.org.codingteam.icfpc.Solver.SolverState
import ru.org.codingteam.icfpc.definitions.{UnitDef, FieldDef}

object Strategist {

  def solution(problem: FieldDef, seed: Int): Seq[Command] = {
    val field = Field.from(problem)
    val units = problem.getUnits(seed).toList
    val emulator = new Emulator(field)
    emulator.load(problem)

    solve(emulator, units)
  }

  private def solve(emulator: Emulator, units: Seq[UnitDef], commands: Seq[Command] = List()): Seq[Command] = {
    def reduce(steps: Seq[SolverState]): Seq[Command] = {
      val cmds = steps.toStream.map(
        step => LocalSolver.findPath(step.field, step.lastMovedUnit.get, step.targetPosition.get))

      val units_ = units.drop(steps.size)
      val commands_ = cmds.takeWhile(c => c.isDefined).flatMap(c => c.get).toVector
      val commandCount = emulator.emulate(commands_)
      if (commandCount == commands_.size) {
        solve(emulator, units_, commands_)
      } else {
        println("!!!Terminated prematurely; please report")
        commands_.take(commandCount)
      }
    }

    val state = SolverState(Field.from(emulator), units)
    val solution = Solver.solution(state)
    solution match {
      case Some(steps) => reduce(steps)
      case None => commands
    }<commit_msg>Initialize emulator unit source.<commit_after>package ru.org.codingteam.icfpc

import ru.org.codingteam.icfpc.Solver.SolverState
import ru.org.codingteam.icfpc.definitions.{UnitDef, FieldDef}

object Strategist {

  def solution(problem: FieldDef, seed: Int): Seq[Command] = {
    val field = Field.from(problem)
    val units = problem.getUnits(seed).toList
    val emulator = new Emulator(field)
    emulator.load(problem)
    emulator.initSource(seed)

    solve(emulator, units)
  }

  private def solve(emulator: Emulator, units: Seq[UnitDef], commands: Seq[Command] = List()): Seq[Command] = {
    def reduce(steps: Seq[SolverState]): Seq[Command] = {
      val cmds = steps.toStream.map(
        step => LocalSolver.findPath(step.field, step.lastMovedUnit.get, step.targetPosition.get))

      val units_ = units.drop(steps.size)
      val commands_ = cmds.takeWhile(c => c.isDefined).flatMap(c => c.get).toVector
      val commandCount = emulator.emulate(commands_)
      if (commandCount == commands_.size) {
        solve(emulator, units_, commands_)
      } else {
        println("!!!Terminated prematurely; please report")
        commands_.take(commandCount)
      }
    }

    val state = SolverState(Field.from(emulator), units)
    val solution = Solver.solution(state)
    solution match {
      case Some(steps) => reduce(steps)
      case None => commands
    }
{'fraction_non_alphanumeric': 0.12237375598967933, 'fraction_numerical': 0.0, 'mean_word_length': 3.4859504132231405, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '2492381', 'n_tokens_mistral': 887, 'n_tokens_neox': 862, 'n_words': 178}