Nightmare before Easter

Easter is a holiday whose time is determined with an unnecessarily complicated rule. The first Sunday after the first full moon in Spring. Most people have no other choice than to look its date up in a calendar and trust in the calendar manufacturer. But not anymore! I will stick it to Big Calendar and reveal the secret formula to calculate the date of easter!

from datetime import date

def easter(year: int) -> date:
    y = year
    g = y % 19 + 1                    # golden number
    c = y // 100 + 1                  # century
    x = (3 * c) // 4 - 12             # correction: dropped leap years
    z = (8 * c + 5) // 25 - 5         # correction: synchronize with moon's orbit
    d = (5 * y) // 4 - x - 10         # find sunday
    e = (11 * g + 20 + z - x) % 30    # epact
    if e == 25 and g > 11 or e == 24:
        e += 1
    n = 44 - e                        # full moon in march
    if n < 21:
        n += 30
    n = n + 7 - (d + n) % 7           # advance to next sunday
    month, day = (4, n - 31) if n > 31 else (3, n)

    return date(year, month, day)

My favorite thing about it is that each line becomes more horrendous than the previous.

This algorithm was developed by Lilius and Clavius at the end of the 16th Century. I became aware of it through a mention in an exercise in Donald Knuth’s The Art of Computer Programming 1 (Third edition, p. 159f).

git subtree

Who does not know this common situation: We have a new Idea to extend our existing Project old. So we create a subdirectory newIdea in the corresponding repository. It turns out that the idea was so good that it would also be useful outside of the project old. It would be sensible to create a new repository new which should only contain the subdirectory newIdea. In fact, this problem seems to be so common that there is a special git subcommand since 2012 for this purpose (and more complicated cases): git subtree

New repository from subdirectory

old/
├─ newIdea/
│  ├─ lib.rs
├─ main.rs

So inside the directory old we execute the following command:

git subtree split --prefix=newIdea/ --branch=onlyNewIdeaBranch

This creates a new Branch onlyNewIdeaBranch which only contains the contents of newIdea, i.e., a new root directory. So this branch has a newly written history consisting only of commits with influence on files below of newIdea/.

Now we can create the new repository new and pull the newly created branch.Branch pullen.

cd .. ; mkdir new ; cd new
git init
git pull ../alt onlyNewIdeaBranch

Maybe we want to delete the newIdea subdirectory from the old repository. Probably we have to change infrastructure code in the new repository.

old/
├─ main.rs
new/
├─ lib.rs

Move a subdirectory into an existing repository

Possibly we notice that the code would fit better into an existing repository instead of a new one? Perhaps because we are in the process of moving our code into a monorepo? No problem at all!

old/
├─ newIdea/
│  ├─ lib.rs
├─ main.rs
monorepo/
├─ project1/
├─ project2/

We already created the onlyNewIdeaBranch, which we want to move into the subdirectory goodIdea of the monorepo. Again, we can solve it with git subtree:

cd monorepo
git branch withGoodIdeadBranch
git checkout withGoodIdeadBranch
git subtree add --prefix=goodIdea/ ../old onlyNewIdeaBranch

As soon as we ensured that our new Branch withGoodIdeadBranch looks good and we modified the infrastructure code, we can merge it into main.

old/
├─ neueIdee/
│  ├─ lib.rs
├─ main.rs
monorepo/
├─ project1/
├─ project2/
├─ goodIdea/
│  ├─ lib.rs

compress-pdf

To compress of a pdf with many high resolution images to a sensible filesize (by downscaling and reencoding the images), one can use ghostscript:

gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/ebook -dEmbedAllFonts=true -dSubsetFonts=true -dNOPAUSE -dQUIET -dBATCH -sOutputFile=output.pdf input.pdf

The available presets are screen, ebook, printer, prepress and default. More options can be listed with:

gs -sDEVICE=pdfwrite -o /dev/null -c "currentpagedevice { exch ==only ( ) print == } forall"