Skip to content
advent of code 2015

day 25

let it snow

approach

the codes are filled in along diagonals, so index(r, c) turns a zero-based row and column into the position in that order using triangular numbers. the row and column are read from stdin as two numbers.

from the first code, the loop then applies the multiply-and-mod step that many times. sol.rs is a line-by-line port of the same thing. day 25 has a single part; the second star comes from finishing the others.

the trick

the position on the diagonal walk has a closed form, so only the modular steps need a loop.

python
def index(r, c):
    fst = (r * (r + 1)) // 2
    snd = ((r + c + 1) * (r + c + 2)) // 2
    thd = ((r + 1) * (r + 2)) // 2
    return fst + snd - thd