r/ProgrammerHumor • • 3d ago

Meme technicallyCorrect

Post image
1.5k Upvotes

51 comments sorted by

View all comments

Show parent comment

12

u/etoastie 2d ago

Can simplify the middle two a tad by reflecting the lines at the middle, instead of trying to model the diagonal with the offset

if min(row, col) == 0 || col+1 == WIDTH || row+1 == HEIGHT {
    fmt.printf("#")
} else if col == min(HEIGHT-row-1, row) || col == max(HEIGHT-row, row+1) {
    fmt.printf("#")
} else {
    fmt.printf(" ")
}

3

u/redlaWw 1d ago edited 1d ago

I explicitly mutated the rows in the second half, it made the formulae a bit less messy

for mut y in 0..(width-1) {
    if y > (width-2)/2 {
        y = width-2-y
    }
    for x in 0..width {
        match (x, y) {
            (x, y) if x*y==0 || // assume no overflow
                      x==y ||
                      y==width-1-x ||
                      x==width-1 => print!("#"),
            _ => print!(" ")
        }
    }
    print!("\n");
}

EDIT: Got my x and y backwards ._.

2

u/etoastie 11h ago

fuck it, branchless version

package grid

import "core:fmt"

WIDTH, HEIGHT :: 10, 9

main :: proc() {
  lhs, rhs := 0b1100000000, 0b0000000011
  x := 0b1000000001 // 513

  for row in 0..<HEIGHT {
    for i := uint(0); i < WIDTH; i += 1 {
      // ascii ' '+3 == '#'
      c := ' ' + 3 * rune((x | 513) & (1<<i) > 0 || x == 513)
      fmt.print(c)
    }
    fmt.println()

    // ~ is xor in odin
    x ~= (lhs ~ rhs)
    lhs >>= 1 + uint(lhs == 96)
    rhs <<= 1 + uint(rhs == 24)
  }
}