Day 4: Perilous Pier

Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL

FAQ

  • VegOwOtenks@lemmy.world
    link
    fedilink
    English
    arrow-up
    7
    ·
    18 hours ago

    ARM Assembly

    No static allocation today! Feels more clean, can read arbitrary large inputs. Also massively helps with binary size. After stripping, it’s got only 976 Bytes of size.

    The code is enormous of course, again. Especially because it requires a three-times nested loop. Runs immeasurably fast and has, according to time 384k maxresident memory.

    196 lines of assembly
    .global _start // Declare the entry point of the program
    .align 4      // Align the next instruction to a 4-byte boundary
    
    _start:
      bl slurp
    
      // make space for pattern buffer
      sub x0, x19, x20
      add x0, x0, x19
      mov x8, #214 // brk
      svc 0
      mov x23, x19 // pattern buffer
    
      mov x27, x20
      bl repair
      bl print
    
      mov x0, x27
      bl join_lines
    
      mov x20, x27
      bl repair
      bl print
    
      b exit
    
    // x0: str
    join_lines:
      sub x0, x0, #1
      mov x1, x0 // to
    join_lines_body:
      ldrb w2, [x0, #1]!
      cmp w2, #10 // newline
      b.eq join_lines_body
      strb w2, [x1, #1]!
      cbnz w2, join_lines_body
    
      mov w3, #10 // write final newline
      strb w3, [x1]
      strb wzr, [x1, #1]
      ret
    
    // x0: buffer, w1: byte
    // out x2: offset
    find_byte_index:
      mov x2, #-1
    find_byte_index_again:
      add x2, x2, #1
      ldrb w3, [x0, x2]
      cmp w1, w3
      b.ne find_byte_index_again
      ret
    
    // x0: from, x1: to, x2: n
    copy_bytes:
      b copy_bytes_tail
    copy_bytes_body:
      ldrb w3, [x0], #1
      strb w3, [x1], #1
      sub x2, x2, #1
    copy_bytes_tail:
      cbnz x2, copy_bytes_body
      ret
    
    
    // in: x20: barcode strings
    // out: x0=#instructions
    repair:
      stp x29, x30, [sp, #-16]!
      mov x19, #0
      mov x26, #0 // counter for instructions
    repair_each_barcode_head:
      ldrb w0, [x20]
      cbz w0, repair_done // end of buffer
    repair_each_barcode_body:
      mov x0, x20
      mov x1, #10
      bl find_byte_index // x2: length of barcode
      mov x21, x2
    
    repair_each_divisor_head:
      mov x18, #0 // iterator, divisor
      b repair_each_divisor_tail
    repair_each_divisor_body:
      mov x0, x20
      mov x1, x23
      mov x2, x18
      bl copy_bytes
    
    repair_unify_head:
      mov x24, #-1 // iterator, barcode index
      mov x25, #-1 // iterator, pattern index
      b repair_unify_tail
    repair_unify_body:
      ldrb w0, [x20, x24]
      ldrb w1, [x23, x25]
      cmp w0, w1
      b.eq repair_unify_tail // fallthrough: not equal
    
      cmp w0, #63
      b.eq repair_unify_tail // '?' in barcode always matches
    
      cmp w1, #63 // '?' = 63
      b.ne repair_each_divisor_tail // '?' in pattern must be copied
      strb w0, [x23, x25]
    
    repair_unify_tail:
      // increment iterator vars
      add x24, x24, #1
      add x25, x25, #1
      cmp x25, x18
      csel x25, xzr, x25, eq // wrapping (modulo)
    
      cmp x24, x21 // barcode-index = barcode-length
      b.ne repair_unify_body // fallthrough, pattern did unify
      add x26, x26, x18
      b repair_each_barcode_tail
    
    repair_each_divisor_tail:
      add x18, x18, #1
      cmp x18, x21
      b.le repair_each_divisor_body // fallthrough: not divisor != length
    
    repair_each_barcode_tail:
      add x20, x20, x21
      add x20, x20, #1 // skip the newline
      b repair_each_barcode_head
    
    repair_done:
      ldp x29, x30, [sp], #16
      mov x0, x26
      ret
    
    // out: x19: current brk, x20: buf
    slurp:
      stp x29, x30, [sp, #-16]!
      mov x0, #0
      mov x8, #214 // brk
      svc 0
      mov x19, x0 // current brk
      mov x20, x0 // buf base
    
    slurp_again:
      add x0, x19, #4096
      mov x8, #214 // brk
      svc 0
      mov x19, x0
    
      mov x0, #0 // stdin
      sub x1, x19, #4096
      mov x2, #4096
      mov x8, #0x3F // read
      svc 0
      cbz x0, exit_fail // end of file
      cmp x0, #4096
      b.eq slurp_again
    
      ldp x29, x30, [sp], #16
      sub x1, x19, #4096
      strb wzr, [x1, x0]
      ret
    
    print:
      adr x1, print_buf
      
      mov w2, #10
      strb w2, [x1, #15]!
      mov x4, x1
      mov x5, #1
      
    print_format:
      udiv x1, x0, x2
      smull x3, w1, w2
      sub x3, x0, x3
      add x3, x3, #48
      strb w3, [x4, #-1]!
      add x5, x5, #1
      mov x0, x1
      cbnz x0, print_format
    
    print_write:
      mov x0, #1
      mov x1, x4
      mov x2, x5
      mov x8, #64 // write
      svc 0
    
      ret
    exit:
      mov x0, #0            // x0 = 0 (exit code)
    exit_fail:
      mov x8, #0x5D         // x8 = 93 (sys_exit)
      svc 0
    
    .section .data
    print_buf: .fill 16
    
    • VegOwOtenks@lemmy.world
      link
      fedilink
      arrow-up
      2
      ·
      18 hours ago

      Actually, I did now try more invasive measurements methods but everything jitters so hard it’s basically useless. One run it’s 2ms, another it’s 5ms.