Ponad Murami ← back to the paper

Research paper · Advent 2026

wieniec.py

Source code for replicating the results. It enumerates every admissible candle-lighting schedule and checks which one burns the wreath down most evenly. Running it with the --all option reproduces every number in the paper (takes about 15 s, mostly due to the split into thirds).

python wieniec.py --allPython 3 · CC0raw .py file
#!/usr/bin/env python3
"""
wieniec.py -- replication code for
  "On the Uniform Burning of Advent Wreath Candles"  (czteryswiece.ponadmurami.pl)

Exhaustively enumerates every way of lighting the candles of an Advent wreath
and reports how evenly they burn down.

Model
  * n candles (default 4), n Sundays of Advent.
  * Every Sunday is one session of equal length, optionally split into m equal parts.
  * In every part of Sunday k exactly k candles burn (k = 1..n).
  * All candles burn at the same, constant rate.
  * --pink: candle no. 3 (the pink one, Gaudete) may not burn before Sunday 3
    and must burn at least once on Sunday 3.

Usage
  python wieniec.py                 # whole Sundays, no pink rule
  python wieniec.py -m 2            # Sundays split into halves
  python wieniec.py -m 2 --pink     # halves + pink candle waits for Gaudete
  python wieniec.py --all           # the table from the paper

Ponad Murami, Advent 2026. Public domain (CC0). No candles were harmed.
"""
import argparse
from collections import Counter
from fractions import Fraction
from itertools import combinations, product

PINK = 2  # zero-based index of the pink candle (the third one)


def slots(n, m):
    """All (sunday, part) pairs in chronological order."""
    return [(k, p) for k in range(1, n + 1) for p in range(m)]


def schedules(n, m, pink):
    """Yield every admissible schedule: a tuple of sets of burning candles, one per slot."""
    sl = slots(n, m)
    options = []
    for k, _ in sl:
        opts = list(combinations(range(n), k))
        if pink and k < 3:
            opts = [c for c in opts if PINK not in c]
        options.append(opts)
    for sch in product(*options):
        if pink and not any(PINK in s for s, (k, _) in zip(sch, sl) if k == 3):
            continue
        yield sch


def usage(sch, n):
    """How many parts each candle has burned."""
    u = [0] * n
    for s in sch:
        for c in s:
            u[c] += 1
    return u


def unevenness(u, n, m):
    """(max - min, total deviation from the fair share), both in Sundays."""
    fair = Fraction(m * (n + 1), 2)  # parts per candle if perfectly fair
    spread = Fraction(max(u) - min(u), m)
    dev = sum(abs(x - fair) for x in u) / m
    return spread, dev


def analyse(n, m, pink, show=3):
    total, best, hits, examples = 0, None, Counter(), []
    for sch in schedules(n, m, pink):
        total += 1
        key = unevenness(usage(sch, n), n, m)
        hits[key] += 1
        if best is None or key < best:
            best, examples = key, []
        if key == best and len(examples) < show:
            examples.append(sch)
    return total, best, hits[best], examples


def fmt(sch, n, m):
    names = "ABCD" if n == 4 else [str(i + 1) for i in range(n)]
    out, sl = [], slots(n, m)
    for k in range(1, n + 1):
        parts = [s for s, (kk, _) in zip(sch, sl) if kk == k]
        out.append(f"Sunday {k}: " + " | ".join("".join(names[c] for c in p) for p in parts))
    return "\n    ".join(out)


def report(n, m, pink):
    total, (spread, dev), count, ex = analyse(n, m, pink)
    label = f"n={n}, parts={m}, pink rule={'on' if pink else 'off'}"
    print(f"== {label}")
    print(f"   admissible schedules : {total}")
    print(f"   best spread (Sundays): {spread}   total deviation: {dev}")
    print(f"   schedules achieving it: {count}")
    print("   " + ("PERFECT: all candles burn equally." if spread == 0 else "Perfect equality is IMPOSSIBLE."))
    for sch in ex:
        print("    " + fmt(sch, n, m))
        print("    usage in parts:", usage(sch, n))
    print()


def greedy(n, m):
    """Constructive proof of Theorem 3: always light the k least-burned candles."""
    u = [0] * n
    for k in range(1, n + 1):
        for _ in range(m):
            for c in sorted(range(n), key=lambda i: (u[i], i))[:k]:
                u[c] += 1
    return u


if __name__ == "__main__":
    ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
    ap.add_argument("-n", type=int, default=4, help="number of candles / Sundays (default 4)")
    ap.add_argument("-m", type=int, default=1, help="parts per Sunday (default 1)")
    ap.add_argument("--pink", action="store_true", help="pink candle waits for Gaudete")
    ap.add_argument("--all", action="store_true", help="print the full table from the paper")
    a = ap.parse_args()
    if a.all:
        for m in (1, 2, 3):
            for pink in (False, True):
                report(4, m, pink)
        print("== Theorem 3 (greedy), n candles x m parts: '=' perfect, '~' not")
        for n in range(2, 9):
            print(f"   n={n}: " + " ".join("=" if len(set(greedy(n, m))) == 1 else "~" for m in range(1, 7)))
    else:
        report(a.n, a.m, a.pink)