// File: tanakh[P]icfpc2015/main.cpp
// Function: replay(problem const&, int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&)
void replay(const problem &prob, int seed, const string &moves)
{
  vlog() << "replay:" << endl;
  auto bd = make_board(prob);
  rng r(seed);

  const char *p = moves.c_str();
  int move_score = 0;

  for (int turn = 0; turn < prob.source_length && *p; turn++) {
    const unit &u = prob.units[r.get() % prob.units.size()];
    pt pos = get_init_pos(prob, u);
    int rot = 0;
    if (!check(bd, pos, u, rot))
      break;
    vlog() << "*POP*" << endl;
    print_board_(bd, pos, u, rot);

    set<pair<pair<int,int>, set<pair<int,int>>>> prevs;
    prevs.insert(uposs(u, pos, rot));

    for (;*p;) {
      pt npos = pos;
      int nrot = rot;
      char c = *p++;
      command mov = to_command(c);
      switch(mov) {
      case W:   npos += pt(-2, 0); break;
      case E:   npos += pt(2, 0); break;
      case SW:  npos += pt(-1, 1); break;
      case SE:  npos += pt(1, 1); break;
      case CW:  nrot = (nrot + 1) % u.rot_max; break;
      case CCW: nrot = (nrot + u.rot_max - 1) % u.rot_max; break;
      }

      auto tt = uposs(u, npos, nrot);
      if (prevs.count(tt)) {
        vlog() << "ERROR: duplicate positions " << show_command(mov) << "(" << c << ")" << endl;
        return;
      }
      prevs.insert(tt);

      if (check(bd, npos, u, nrot)) {
        pos = npos;
        rot = nrot;
        vlog() << "cmd: " << show_command(mov) << "(" << c << ")" << endl;
        print_board_(bd, pos, u, rot);
      }
      else {
        vlog() << "cmd: LOCK (" << show_command(mov) << ", " << c << ")" << endl;
        move_score += put_unit(bd, pos, u, rot, 0).first;
        print_board(bd);
        break;
      }
    }
  }

  auto psc = power_score(moves);

  vlog() << "move score: " << move_score << endl;
  vlog() << "power score: " << psc << endl;
  vlog() << "total score: " << move_score + psc << endl;

  if (*p)
    vlog() << "ERROR: sequence remains." << endl;
}

// Assembly:
subq $0x328, %rsp            # imm = 0x328
movq %rdi, 0x320(%rsp)
movl %esi, 0x31c(%rsp)
movq %rdx, 0x310(%rsp)
callq 0x5040
movq %rax, %rdi
leaq 0x29198(%rip), %rsi     # 0x36655
callq 0x43c0
movq %rax, %rdi
movq 0x3cac4(%rip), %rsi     # 0x49f90
callq 0x4400
movq 0x320(%rsp), %rsi
leaq 0x2f8(%rsp), %rdi
callq 0x96b0
movl 0x31c(%rsp), %esi
leaq 0x2f4(%rsp), %rdi
callq 0x13bb0
jmp 0xd4fc
movq 0x310(%rsp), %rdi
callq 0x4130
movq %rax, 0x2d8(%rsp)
movl $0x0, 0x2d4(%rsp)
movl $0x0, 0x2d0(%rsp)
movl 0x2d0(%rsp), %ecx
movq 0x320(%rsp), %rdx
xorl %eax, %eax
cmpl 0x40(%rdx), %ecx
movb %al, 0x12b(%rsp)
jge 0xd559
movq 0x2d8(%rsp), %rax
cmpb $0x0, (%rax)
setne %al
movb %al, 0x12b(%rsp)
movb 0x12b(%rsp), %al
testb $0x1, %al
jne 0xd569
jmp 0xdec8
movq 0x320(%rsp), %rax
addq $0x8, %rax
movq %rax, 0x118(%rsp)
leaq 0x2f4(%rsp), %rdi
callq 0x13bd0
movl %eax, 0x124(%rsp)
jmp 0xd593
movl 0x124(%rsp), %eax
cltq
movq %rax, 0x108(%rsp)
movq 0x320(%rsp), %rdi
addq $0x8, %rdi
callq 0x13c00
movq 0x118(%rsp), %rdi
movq %rax, %rcx
movq 0x108(%rsp), %rax
xorl %edx, %edx
divq %rcx
movq %rdx, %rsi
callq 0x13890
movq %rax, 0x2c8(%rsp)
movq 0x320(%rsp), %rdi
movq 0x2c8(%rsp), %rsi
callq 0x7a10
movq %rax, 0x110(%rsp)
jmp 0xd5fc
movq 0x110(%rsp), %rax
movq %rax, 0x2c0(%rsp)
movl $0x0, 0x2bc(%rsp)
movq 0x2c8(%rsp), %rdx
movl 0x2bc(%rsp), %ecx
leaq 0x2f8(%rsp), %rdi
leaq 0x2c0(%rsp), %rsi
callq 0x6e60
movb %al, 0x107(%rsp)
jmp 0xd644
movb 0x107(%rsp), %al
testb $0x1, %al
jne 0xd66f
jmp 0xd651
jmp 0xdec8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2e8(%rsp)
movl %eax, 0x2e4(%rsp)
jmp 0xe01e
callq 0x5040
movq %rax, %rdi
leaq 0x28fdf(%rip), %rsi     # 0x3665d
callq 0x43c0
movq %rax, 0xf8(%rsp)
jmp 0xd68d
movq 0xf8(%rsp), %rdi
movq 0x3c8f4(%rip), %rsi     # 0x49f90
callq 0x4400
jmp 0xd6a3
movq 0x2c8(%rsp), %rdx
movl 0x2bc(%rsp), %ecx
leaq 0x2f8(%rsp), %rdi
leaq 0x2c0(%rsp), %rsi
callq 0x7630
jmp 0xd6c9
leaq 0x288(%rsp), %rdi
callq 0x14de0
movq 0x2c8(%rsp), %rsi
movl 0x2bc(%rsp), %ecx
leaq 0x250(%rsp), %rdi
leaq 0x2c0(%rsp), %rdx
callq 0xa420
jmp 0xd6fc
leaq 0x288(%rsp), %rdi
leaq 0x250(%rsp), %rsi
callq 0x14df0
movb %dl, 0xef(%rsp)
movq %rax, 0xf0(%rsp)
jmp 0xd722
movb 0xef(%rsp), %al
movq 0xf0(%rsp), %rcx
movq %rcx, 0x240(%rsp)
movb %al, 0x248(%rsp)
leaq 0x250(%rsp), %rdi
callq 0x14e50
movq 0x2d8(%rsp), %rax
cmpb $0x0, (%rax)
je 0xde75
movq 0x2c0(%rsp), %rax
movq %rax, 0x238(%rsp)
movl 0x2bc(%rsp), %eax
movl %eax, 0x234(%rsp)
movq 0x2d8(%rsp), %rax
movq %rax, %rcx
incq %rcx
movq %rcx, 0x2d8(%rsp)
movb (%rax), %al
movb %al, 0x233(%rsp)
movsbl 0x233(%rsp), %edi
callq 0x6a20
movl %eax, 0xe8(%rsp)
jmp 0xd7b1
movl 0xe8(%rsp), %eax
movl %eax, 0x22c(%rsp)
movl 0x22c(%rsp), %eax
movq %rax, 0xe0(%rsp)
subq $0x5, %rax
ja 0xd9b7
movq 0xe0(%rsp), %rax
leaq 0x28941(%rip), %rcx     # 0x36128
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2e8(%rsp)
movl %eax, 0x2e4(%rsp)
jmp 0xdeb6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2e8(%rsp)
movl %eax, 0x2e4(%rsp)
leaq 0x250(%rsp), %rdi
callq 0x14e50
jmp 0xdeb6
movl $0xfffffffe, 0x220(%rsp) # imm = 0xFFFFFFFE
movl $0x0, 0x21c(%rsp)
leaq 0x224(%rsp), %rdi
leaq 0x220(%rsp), %rsi
leaq 0x21c(%rsp), %rdx
callq 0x11530
jmp 0xd864
leaq 0x238(%rsp), %rdi
leaq 0x224(%rsp), %rsi
callq 0x127b0
jmp 0xd87b
jmp 0xd9b7
movl $0x2, 0x210(%rsp)
movl $0x0, 0x20c(%rsp)
leaq 0x214(%rsp), %rdi
leaq 0x210(%rsp), %rsi
leaq 0x20c(%rsp), %rdx
callq 0x11530
jmp 0xd8b5
leaq 0x238(%rsp), %rdi
leaq 0x214(%rsp), %rsi
callq 0x127b0
jmp 0xd8cc
jmp 0xd9b7
movl $0xffffffff, 0x200(%rsp) # imm = 0xFFFFFFFF
movl $0x1, 0x1fc(%rsp)
leaq 0x204(%rsp), %rdi
leaq 0x200(%rsp), %rsi
leaq 0x1fc(%rsp), %rdx
callq 0x11530
jmp 0xd906
leaq 0x238(%rsp), %rdi
leaq 0x204(%rsp), %rsi
callq 0x127b0
jmp 0xd91d
jmp 0xd9b7
movl $0x1, 0x1f0(%rsp)
movl $0x1, 0x1ec(%rsp)
leaq 0x1f4(%rsp), %rdi
leaq 0x1f0(%rsp), %rsi
leaq 0x1ec(%rsp), %rdx
callq 0x11530
jmp 0xd957
leaq 0x238(%rsp), %rdi
leaq 0x1f4(%rsp), %rsi
callq 0x127b0
jmp 0xd96e
jmp 0xd9b7
movl 0x234(%rsp), %eax
addl $0x1, %eax
movq 0x2c8(%rsp), %rcx
cltd
idivl 0x20(%rcx)
movl %edx, 0x234(%rsp)
jmp 0xd9b7
movl 0x234(%rsp), %eax
movq 0x2c8(%rsp), %rcx
addl 0x20(%rcx), %eax
subl $0x1, %eax
movq 0x2c8(%rsp), %rcx
cltd
idivl 0x20(%rcx)
movl %edx, 0x234(%rsp)
movq 0x2c8(%rsp), %rsi
movl 0x234(%rsp), %ecx
leaq 0x1b0(%rsp), %rdi
leaq 0x238(%rsp), %rdx
callq 0xa420
jmp 0xd9dd
leaq 0x288(%rsp), %rdi
leaq 0x1b0(%rsp), %rsi
callq 0x14e70
movq %rax, 0xd8(%rsp)
jmp 0xd9fc
movq 0xd8(%rsp), %rax
cmpq $0x0, %rax
je 0xdb2e
callq 0x5040
movq %rax, %rdi
leaq 0x28c46(%rip), %rsi     # 0x36663
callq 0x43c0
movq %rax, 0xd0(%rsp)
jmp 0xda2c
movl 0x22c(%rsp), %esi
leaq 0x190(%rsp), %rdi
callq 0x67e0
jmp 0xda42
movq 0xd0(%rsp), %rdi
leaq 0x190(%rsp), %rsi
callq 0x4360
movq %rax, 0xc8(%rsp)
jmp 0xda61
movq 0xc8(%rsp), %rdi
leaq 0x28c1f(%rip), %rsi     # 0x3668f
callq 0x43c0
movq %rax, 0xc0(%rsp)
jmp 0xda7f
movq 0xc0(%rsp), %rdi
movsbl 0x233(%rsp), %esi
callq 0x4480
movq %rax, 0xb8(%rsp)
jmp 0xda9e
movq 0xb8(%rsp), %rdi
leaq 0x28ebe(%rip), %rsi     # 0x3696b
callq 0x43c0
movq %rax, 0xb0(%rsp)
jmp 0xdabc
movq 0xb0(%rsp), %rdi
movq 0x3c4c5(%rip), %rsi     # 0x49f90
callq 0x4400
jmp 0xdad2
leaq 0x190(%rsp), %rdi
callq 0x4700
movl $0x1, 0x18c(%rsp)
jmp 0xde38
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2e8(%rsp)
movl %eax, 0x2e4(%rsp)
jmp 0xde66
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2e8(%rsp)
movl %eax, 0x2e4(%rsp)
leaq 0x190(%rsp), %rdi
callq 0x4700
jmp 0xde66
leaq 0x288(%rsp), %rdi
leaq 0x1b0(%rsp), %rsi
callq 0x14ed0
movb %dl, 0xa7(%rsp)
movq %rax, 0xa8(%rsp)
jmp 0xdb54
movb 0xa7(%rsp), %al
movq 0xa8(%rsp), %rcx
movq %rcx, 0x178(%rsp)
movb %al, 0x180(%rsp)
movq 0x2c8(%rsp), %rdx
movl 0x234(%rsp), %ecx
leaq 0x2f8(%rsp), %rdi
leaq 0x238(%rsp), %rsi
callq 0x6e60
movb %al, 0xa6(%rsp)
jmp 0xdb9f
movb 0xa6(%rsp), %al
testb $0x1, %al
jne 0xdbaf
jmp 0xdce9
movq 0x238(%rsp), %rax
movq %rax, 0x2c0(%rsp)
movl 0x234(%rsp), %eax
movl %eax, 0x2bc(%rsp)
callq 0x5040
movq %rax, %rdi
leaq 0x28aa3(%rip), %rsi     # 0x3667f
callq 0x43c0
movq %rax, 0x98(%rsp)
jmp 0xdbeb
movl 0x22c(%rsp), %esi
leaq 0x158(%rsp), %rdi
callq 0x67e0
jmp 0xdc01
movq 0x98(%rsp), %rdi
leaq 0x158(%rsp), %rsi
callq 0x4360
movq %rax, 0x90(%rsp)
jmp 0xdc20
movq 0x90(%rsp), %rdi
leaq 0x28a60(%rip), %rsi     # 0x3668f
callq 0x43c0
movq %rax, 0x88(%rsp)
jmp 0xdc3e
movq 0x88(%rsp), %rdi
movsbl 0x233(%rsp), %esi
callq 0x4480
movq %rax, 0x80(%rsp)
jmp 0xdc5d
movq 0x80(%rsp), %rdi
leaq 0x28cff(%rip), %rsi     # 0x3696b
callq 0x43c0
movq %rax, 0x78(%rsp)
jmp 0xdc78
movq 0x78(%rsp), %rdi
movq 0x3c30c(%rip), %rsi     # 0x49f90
callq 0x4400
jmp 0xdc8b
leaq 0x158(%rsp), %rdi
callq 0x4700
movq 0x2c8(%rsp), %rdx
movl 0x2bc(%rsp), %ecx
leaq 0x2f8(%rsp), %rdi
leaq 0x2c0(%rsp), %rsi
callq 0x7630
jmp 0xdcbe
jmp 0xde2d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2e8(%rsp)
movl %eax, 0x2e4(%rsp)
leaq 0x158(%rsp), %rdi
callq 0x4700
jmp 0xde66
callq 0x5040
movq %rax, %rdi
leaq 0x2898d(%rip), %rsi     # 0x36685
callq 0x43c0
movq %rax, 0x70(%rsp)
jmp 0xdd04
movl 0x22c(%rsp), %esi
leaq 0x138(%rsp), %rdi
callq 0x67e0
jmp 0xdd1a
movq 0x70(%rsp), %rdi
leaq 0x138(%rsp), %rsi
callq 0x4360
movq %rax, 0x68(%rsp)
jmp 0xdd33
movq 0x68(%rsp), %rdi
leaq 0x286d0(%rip), %rsi     # 0x3640f
callq 0x43c0
movq %rax, 0x60(%rsp)
jmp 0xdd4b
movq 0x60(%rsp), %rdi
movsbl 0x233(%rsp), %esi
callq 0x4480
movq %rax, 0x58(%rsp)
jmp 0xdd64
movq 0x58(%rsp), %rdi
leaq 0x28bfb(%rip), %rsi     # 0x3696b
callq 0x43c0
movq %rax, 0x50(%rsp)
jmp 0xdd7c
movq 0x50(%rsp), %rdi
movq 0x3c208(%rip), %rsi     # 0x49f90
callq 0x4400
jmp 0xdd8f
leaq 0x138(%rsp), %rdi
callq 0x4700
movq 0x2c8(%rsp), %rdx
movl 0x2bc(%rsp), %ecx
leaq 0x2f8(%rsp), %rdi
leaq 0x2c0(%rsp), %rsi
xorl %r8d, %r8d
callq 0x6f10
movq %rax, 0x48(%rsp)
jmp 0xddca
movq 0x48(%rsp), %rax
movq %rax, 0x130(%rsp)
movl 0x130(%rsp), %ecx
movl 0x2d4(%rsp), %eax
addl %ecx, %eax
movl %eax, 0x2d4(%rsp)
leaq 0x2f8(%rsp), %rdi
callq 0x7440
jmp 0xddfd
movl $0x5, 0x18c(%rsp)
jmp 0xde38
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2e8(%rsp)
movl %eax, 0x2e4(%rsp)
leaq 0x138(%rsp), %rdi
callq 0x4700
jmp 0xde66
movl $0x0, 0x18c(%rsp)
leaq 0x1b0(%rsp), %rdi
callq 0x14e50
movl 0x18c(%rsp), %eax
movl %eax, 0x44(%rsp)
testl %eax, %eax
je 0xde61
jmp 0xde56
movl 0x44(%rsp), %eax
subl $0x5, %eax
je 0xde75
jmp 0xde80
jmp 0xd74d
leaq 0x1b0(%rsp), %rdi
callq 0x14e50
jmp 0xdeb6
movl $0x0, 0x18c(%rsp)
leaq 0x288(%rsp), %rdi
callq 0x14f30
movl 0x18c(%rsp), %eax
testl %eax, %eax
jne 0xe009
jmp 0xde9e
jmp 0xdea0
movl 0x2d0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2d0(%rsp)
jmp 0xd527
leaq 0x288(%rsp), %rdi
callq 0x14f30
jmp 0xe01e
movq 0x310(%rsp), %rdi
callq 0x9b40
movl %eax, 0x40(%rsp)
jmp 0xdedb
movl 0x40(%rsp), %eax
movl %eax, 0x12c(%rsp)
callq 0x5040
movq %rax, %rdi
leaq 0x2879c(%rip), %rsi     # 0x36691
callq 0x43c0
movq %rax, 0x38(%rsp)
jmp 0xdf01
movq 0x38(%rsp), %rdi
movl 0x2d4(%rsp), %esi
callq 0x4600
movq %rax, 0x30(%rsp)
jmp 0xdf19
movq 0x30(%rsp), %rdi
movq 0x3c06b(%rip), %rsi     # 0x49f90
callq 0x4400
jmp 0xdf2c
callq 0x5040
movq %rax, %rdi
leaq 0x28763(%rip), %rsi     # 0x3669e
callq 0x43c0
movq %rax, 0x28(%rsp)
jmp 0xdf47
movq 0x28(%rsp), %rdi
movl 0x12c(%rsp), %esi
callq 0x4600
movq %rax, 0x20(%rsp)
jmp 0xdf5f
movq 0x20(%rsp), %rdi
movq 0x3c025(%rip), %rsi     # 0x49f90
callq 0x4400
jmp 0xdf72
callq 0x5040
movq %rax, %rdi
leaq 0x2872b(%rip), %rsi     # 0x366ac
callq 0x43c0
movq %rax, 0x18(%rsp)
jmp 0xdf8d
movq 0x18(%rsp), %rdi
movl 0x2d4(%rsp), %esi
movl 0x12c(%rsp), %eax
addl %eax, %esi
callq 0x4600
movq %rax, 0x10(%rsp)
jmp 0xdfae
movq 0x10(%rsp), %rdi
movq 0x3bfd6(%rip), %rsi     # 0x49f90
callq 0x4400
jmp 0xdfc1
movq 0x2d8(%rsp), %rax
cmpb $0x0, (%rax)
je 0xdffe
callq 0x5040
movq %rax, %rdi
leaq 0x286dd(%rip), %rsi     # 0x366ba
callq 0x43c0
movq %rax, 0x8(%rsp)
jmp 0xdfe9
movq 0x8(%rsp), %rdi
movq 0x3bf9b(%rip), %rsi     # 0x49f90
callq 0x4400
jmp 0xdffc
jmp 0xdffe
movl $0x0, 0x18c(%rsp)
leaq 0x2f8(%rsp), %rdi
callq 0x12350
addq $0x328, %rsp            # imm = 0x328
retq
leaq 0x2f8(%rsp), %rdi
callq 0x12350
movq 0x2e8(%rsp), %rdi
callq 0x4620
nopl (%rax,%rax)