Skip to content

Instantly share code, notes, and snippets.

use std::cmp;
fn spiral_distance(num: u32) -> u32 {
let ring = (((num as f64).sqrt() - 1.) / 2.).ceil() as u32;
let root = (num as f64 - 1.).sqrt() as u32;
let offset_to_mid = root / 2;
let ring_min = root * root + 1 + offset_to_mid;
let ring_max = (root + 1) * (root + 1) - offset_to_mid;
return ring + cmp::min(
(num as i32 - ring_min as i32).abs(),
@fay59
fay59 / Quirks of C.md
Last active April 3, 2025 02:27
Quirks of C

Here's a list of mildly interesting things about the C language that I learned mostly by consuming Clang's ASTs. Although surprises are getting sparser, I might continue to update this document over time.

There are many more mildly interesting features of C++, but the language is literally known for being weird, whereas C is usually considered smaller and simpler, so this is (almost) only about C.

1. Combined type and variable/field declaration, inside a struct scope [https://godbolt.org/g/Rh94Go]

struct foo {
   struct bar {
 int x;