23

Day 3: Foggy Forest

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

you are viewing a single comment's thread
view the rest of the comments
[-] VegOwOtenks@lemmy.world 7 points 1 day ago

ARM Assembly

Runs instantly. Executable is 46K of disk. I could probably use dynamic allocation to reduce the executable size. But I'm happy, satisfied and not sure whether I can do this tomorrow.

a lot of code

.global _start // Declare the entry point of the program
.align 4      // Align the next instruction to a 4-byte boundary

_start:
  adr x19, trees
  adr x20, grid
  bl parse
  add x21, x21, #1 // account for the newline stride
  bl count
  mov x0, x23
  bl print
  mov x0, x24
  bl print
  b exit

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

// x19: array, x20: grid, x21: nrows & ncols
// out: x23 squares, x24 rot-squares
count:
  sub x19, x19, #8 // outer iteration pointer
  mov x23, #0
  mov x24, #0

count_outer_head:
  b count_outer_tail

count_outer_body:
count_inner_head:
  mov x22, x19 // inner iteration pointer
  b count_inner_tail

count_inner_body:
  cmp w0, w2 // point2.y <= point1.y
  b.gt count_inner_tail // continue
  cmp w3, w1 // point2.x <= point1.x
  b.le count_inner_tail // continue

  sub w4, w2, w0 // y-difference
  sub w5, w3, w1 // x-difference

  // bot-left
  add w6, w0, w5 // bot-left.y = point1.y + x-diff
  sub w7, w1, w4 // point1.x - y-diff
  mul w8, w6, w21
  add w8, w8, w7

  cmp w6, w21
  b.ge count_inner_tail // bot-left.y >= nrows
  cmp w7, #0
  b.lt count_inner_tail // bot-left.x < 0
  ldrb w9, [x20, w8, sxtw]
  cbz w9, count_inner_tail // bot-left is not tree

  // bot-right
  add w6, w2, w5 // bot-right.y = top-right.y + x-diff
  sub w7, w3, w4 // bot-right.x = top-right.x - y-diff
  mul w8, w6, w21
  add w8, w8, w7

  cmp w6, w21
  b.ge count_inner_tail // bot-right.y >= nrows
  cmp w7, #0
  b.lt count_inner_tail // < 0
  ldrb w9, [x20, w8, sxtw]
  cbz w9, count_inner_tail

count_success:
  add x24, x24, #1
  cbnz w4, count_inner_tail
  add x23, x23, #1
  


count_inner_tail:
  ldp w2, w3, [x22, #8]!
  cmp w2, #-1 // sentinel value
  b.eq count_outer_tail
  b count_inner_body

count_outer_tail:
  ldp w0, w1, [x19, #8]!
  cmp w0, #-1 // sentinel value
  b.ne count_outer_body
  ret

parse:
  mov x21, #0 // row
  mov x22, #0 // col
  mov x23, x19 // array + i - 1
  sub x23, x23, #8

// parse_read:
  mov x0, #0 // stdin
  mov x1, x20
  mov x2, #32768
  mov x8, #0x3F // read
  svc 0
  cbz x0, exit_fail // end of file

  mov x1, #0 // i
parse_loop_top:
  ldrb w2, [x20, x1]
  cmp w2, #35
  b.eq parse_tree
  cmp w2, #10
  b.eq parse_newline
  b parse_dot
parse_tree:
  mov w2, #1 // nonzero for tree
  stp w21, w22, [x23, #8]!
  b parse_loop_bot
parse_newline:
  add x21, x21, #1
  mov x22, #-1 // fallthrough for zero
parse_dot:
  mov w2, #0 // zero for non-tree
parse_loop_bot:
  strb w2, [x20, x1]
  add x22, x22, #1
  add x1, x1, #1
  cmp x1, x0
  b.ne parse_loop_top

parse_done:
  mov x22, #-1
  stp w22, w22, [x23, #8]
  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
trees: .fill 3*4096
grid: .fill 8*4096

[-] VegOwOtenks@lemmy.world 4 points 1 day ago

Runs unchanged on my Raspberry Pi. I love it.

this post was submitted on 03 Dec 2026
23 points (100.0% liked)

Advent Of Code

1278 readers
56 users here now

An unofficial home for the advent of code community on programming.dev! Other challenges are also welcome!

Advent of Code is an annual Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.

Everybody Codes is another collection of programming puzzles with seasonal events.

EC 2025

AoC 2026

Solution Threads

M T W T F S S
1 2 3 4 5 6
7 8 9 10 11 12

Visualisations Megathread

Rules/Guidelines

Relevant Communities

Relevant Links

Credits

Icon base by Lorc under CC BY 3.0 with modifications to add a gradient

console.log('Hello World')

founded 3 years ago
MODERATORS