aoc 2024 01

loosely based on Advent of Code 2024 - 01

trying to remember how turtle toy thing even works

Log in to post a comment.

Canvas.setpenopacity(-1.0);

const t = new Turtle();

function draw_cases(x1, x2, y1, y2, cx, cy, n_levels) {
    t.penup();
    t.goto(x1, y1);
    t.pendown();

    t.goto(x2, y1);
    t.goto(x2, y2);
    t.goto(x1, y2);
    t.goto(x1, y1);

    t.goto(cx, cy);

    t.penup();
    t.goto(x2, y1);
    t.pendown();
    t.goto(cx, cy);
    
    const n = 10;
    const alpha = 0.8;
    
    if (x1 > cx) {
        for (let i = 1; i <= n_levels; i++) {
            let y = Math.floor(y1 + (y2 - y1) / 3 * i);
            let ny = Math.floor(y1 + (y2 - y1) / 3 * (i - 1));
            
            t.penup();
            t.goto(x2, y);
            t.pendown();
            t.goto(x1, y);
            t.goto(cx, cy);

            const W = x1 - cx;
            let w = W;
            
            for (let i = 0; i < n; i++) {
                w_prv = w;
                w *= alpha;
                
                h1 = (y - cy) / (x1 - cx) * w;
                h2 = (ny - cy) / (x1 - cx) * w;
                
                t.penup();
                t.goto(cx + w, cy + h1);
                t.pendown();
                t.goto(cx + w, cy + h2);
                
                t.goto(cx + w, cy + h1);
                t.goto(cx + w_prv, cy + h1);
            }
        }
    }

    if (x2 < cx) {
        for (let i = 1; i <= n_levels; i++) {
            let y = Math.floor(y1 + (y2 - y1) / 3 * i);
            let ny = Math.floor(y1 + (y2 - y1) / 3 * (i - 1));
            
            t.penup();
            t.goto(x1, y);
            t.pendown();
            t.goto(x2, y);
            t.goto(cx, cy);

            const W = cx - x2;
            let w = W;
            
            for (let i = 0; i < n; i++) {
                w_prv = w;
                w *= alpha;
                
                h1 = (y - cy) / W * w;
                h2 = (ny - cy) / W * w;
                
                t.penup();
                t.goto(cx - w, cy + h1);
                t.pendown();
                t.goto(cx - w, cy + h2);
                
                t.goto(cx - w, cy + h1);
                t.goto(cx - w_prv, cy + h1);
            }
        }
    }

    for (let y = -100; y <= y1; y += 1) {
        let px1 = cx + (x1 - cx) / (y1 - cy) * (y - cy);
        let px2 = cx + (x2 - cx) / (y1 - cy) * (y - cy);
        
        t.penup();
        t.goto(px1, y);
        t.pendown();
        t.goto(px2, y);
    }
    

}

const cx = 0, cy = -102;
const w = 66, h = 133;

draw_cases(-100, -100 + w, 100 - h, 100, cx, cy, 3);
draw_cases(100 - w, 100, 100 - h, 100, cx, cy, 3);

draw_cases(-100 - 2 * w, -100 - w, 100 - h, 100, cx, cy, 1);
draw_cases(100 + w, 100 + 2 * w, 100 - h, 100, cx, cy, 1);

draw_cases(-w/2, w/2, 100, 100 + h, cx, cy, 0);

for (let i = 1; i < 30; i++) {
    draw_cases(-100 - 2 * w * (i + 1), -100 - 2 * w * (i + 1) + w, 100 - h, 100, cx, cy, 0);
    draw_cases(100 + 2 * w * (i + 1) - w, 100 + 2 * w * (i + 1), 100 - h, 100, cx, cy, 0);
}