Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vVvVv-009mbU-17 for pgsql-general@arkaria.postgresql.org; Wed, 17 Dec 2025 17:40:40 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vVvVu-00FKJ8-0g for pgsql-general@arkaria.postgresql.org; Wed, 17 Dec 2025 17:40:38 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vVvVt-00FKJ0-2p for pgsql-general@lists.postgresql.org; Wed, 17 Dec 2025 17:40:38 +0000 Received: from mail-yw1-x112e.google.com ([2607:f8b0:4864:20::112e]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1vVvVs-001Goo-1T for pgsql-general@lists.postgresql.org; Wed, 17 Dec 2025 17:40:38 +0000 Received: by mail-yw1-x112e.google.com with SMTP id 00721157ae682-78d60c6cc17so47954487b3.0 for ; Wed, 17 Dec 2025 09:40:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1765993235; x=1766598035; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=Ps9C43PEf47zrm809Sm5k7ZWV8RPVAzTRIZ3mghY38E=; b=R0spc3vt6M5z4BbKVc8xKKK0gxWnBMNqyoAnaS1E8EcV8I0inzFWRNxR4opWrt4YGa Inp7StG96y3b9mOKz1cGY//UWqeflu/D0AIrwZudWnbYlbWxXCe7k11LeCjBf+9Bp3fb l7cJ4mqv5/1KqtrjPPL6vJ9qWFDeC+bK50qq/zNBUUVkS+Sg3gAIh2549E/gPVb5lC8Z Qqfh2UeTlSl4VGR3Itg8pORfhVQ7AdUeTHzbgh4KEmujZdFQkfPoBt4RAJSeKpRQofSI K6IO4Qv0aEq4P9VHimIOMgAcWYeseVtEgs6MxGnk/v/uom8FFglz8ho+eTEqljK2vM97 WJRg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1765993235; x=1766598035; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=Ps9C43PEf47zrm809Sm5k7ZWV8RPVAzTRIZ3mghY38E=; b=w5YVQ0IidL+2k9H958obn60wPKI60Y5B6nDlus7E0sAz2sk6Ije1K38f0RFTf5JJt0 6sRHMblFy8uo933VHK3JMtC7XyEWwrNFoDiD/dpP9b88GLdVDMdRcYZbEphaA/9r4h6S r6Vw6ZlQl+IaBL9yrEhIO1airL2u+K8Cmu1++3ptBn7amIm7zArp/y86bUJSkQ8tULbS 7z2jd3mzijdpB9ZXyk+XDs35CvEZtBk6IlWQphUwYtQgQbTDXJ/0WBWiM8mN82YdZKNg tR4Lf/eYMBvqd4mspzFsL4Fv5gkQDyMAsfwFxdUDbi4M7RwgcZB9pAvBTZzIqx6ZBJuO Y64w== X-Gm-Message-State: AOJu0Ywy14QeXcI4T3kJroXxnrb3rMxVSnj/2C8Yny6hfYdQmqtK4CFl 0sCJoYu3rnjwINxvfhjRxcaFF6WwrrDIwR3hkminD3/3TfJDaPzNi0dRYZiHxEOXbJEsmik5YgB sVUUe5qZoJOsI6ma8rJosVApsClfgi82fLA== X-Gm-Gg: AY/fxX7MibLyhzmPgDSC1lBHr4HK++UtNuLzbWolAju8AolxOWVZ62TNwqw5DPNIzrf ccpn0UUwxR8GXWhkDw/VMwXR69F+xNNMg5aV1DX663adKBSpjaFz+Rs0vAcFsSvpl7wJBL1ZEGi /bPMq6Pe7HWVYjZofdDQrorvZLDs1k8g1HifRi+4o8iuYicnKXW3s1acaSUFjfBAa9xE5VQkXAx 0jNAaRrsANBqnjl2CR45PiwPhG7FJGQYvX4AJ/ZTAzdgPOXV+i6+X27RNTOcf6J1SMRXBtGB64M mkfMKl41cVvkjeTPd/Rx X-Google-Smtp-Source: AGHT+IELwQFLXzSZAvTNhCOHG2VF+B6D1dp7bqwbFFbsaVSXzvn5qJQCETGhn0/x2L4jIiBUQGY25Ng3zYklvK3XBVk= X-Received: by 2002:a05:690c:4b07:b0:78c:3320:9c4b with SMTP id 00721157ae682-78e66c27c1bmr147347427b3.44.1765993235053; Wed, 17 Dec 2025 09:40:35 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Bernice Southey Date: Wed, 17 Dec 2025 17:39:59 +0000 X-Gm-Features: AQt7F2q8yUNsFD0UfQwQaejdcndxMF8PdlK7Y6ArzowTK4zmKqw9m_CqcZzfpW4 Message-ID: Subject: Re: Advent of Code Day 8 To: Greg Sabino Mullane Cc: pgsql-general@lists.postgresql.org Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk It's slow at 20 seconds, but I'm pleased I finally found a good enough way to use tables for day 8. Afterall, the reason I tried AoC in postgres is because I really like table logic. By swapping out two temp tables and doing insert only, I can avoid the update MVCC bloat that wrecked my previous attempt. It was very educational watching the loop speed degrade to a crawl after a thousand update runs, even though the table never got bigger than 1000 rows. I copied the input into d8(t text). create temp table w1(c int, b text); create temp table w2(c int, b text); do $$ declare r record; begin --loop through the connections in closest order for r in (with j as ( select row_number() over () r, t, split_part(t, ',', 1)::int8 x, split_part(t, ',', 2)::int8 y, split_part(t, ',', 3)::int8 z from d8) select row_number() over( order by (j.x-j1.x)^2 + (j.y-j1.y)^2 + (j.z-j1.z)^2) i, j.t b1, j1.t b2, j.x * j1.x s from j, j j1 where j1.r > j.r) loop --add the two boxes from the current connection insert into w1 values (r.i, r.b1), (r.i, r.b2); --connect all the boxes in the circuits of these two boxes insert into w1 select r.i, w3.b from w1 join w2 on w1.b = w2.b join w2 w3 on w2.c = w3.c; --keep all the existing boxes with their current circuits insert into w1 select * from w2; truncate w2; --get the latest circuit per box insert into w2 select distinct on (b) * from w1 order by b, c desc; truncate w1; --the circuit is complete when all the boxes are in the current circuit if (select count(*) from w2 where c = r.i) = 1000 then raise notice '%', r.s; exit; end if; end loop; end $$;