01020304050607080910111213141516171819202122232425

Advent of Code

2015/20

Infinite Elves and Infinite Houses

in C#

by encse

To keep the Elves busy, Santa has them deliver some presents by hand, door-to-door. He sends them down a street with infinite houses numbered sequentially: 1, 2, 3, 4, 5, and so on.

Each Elf is assigned a number, too, and delivers presents to houses based on that number:

Read the full puzzle.

namespace AdventOfCode.Y2015.Day20;

[ProblemName("Infinite Elves and Infinite Houses")]
class Solution : Solver {

    public object PartOne(string input) {
        var l = int.Parse(input);
        return PresentsByHouse(1000000, 10, l);
    }

    public object PartTwo(string input) {
        var l = int.Parse(input);
        return PresentsByHouse(50, 11, l);
    }

    int PresentsByHouse(int steps, int mul, int l) {
        var presents = new int[1000000];
        for (var i = 1; i < presents.Length; i++) {
            var j = i;
            var step = 0;
            while (j < presents.Length && step < steps) {
                presents[j] += mul * i;
                j += i;
                step++;
            }
        }

        for (var i = 0; i < presents.Length; i++) {
            if (presents[i] >= l) {
                return i;
            }
        }
        return -1;

    }
}

Please ☆ my repo if you like it!

© 2025 Advent of Code is a registered trademark in the US Images provided by Bing image creator