01020304050607080910111213141516171819202122232425

Advent of Code

2020/15

Rambunctious Recitation

in C#

by encse

You catch the airport shuttle and try to book a new flight to your vacation island. Due to the storm, all direct flights have been cancelled, but a route is available to get around the storm. You take it.

While you wait for your flight, you decide to check in with the Elves back at the North Pole. They're playing a memory game and are ever so excited to explain the rules!

Read the full puzzle.

using System.Linq;

namespace AdventOfCode.Y2020.Day15;

[ProblemName("Rambunctious Recitation")]
class Solution : Solver {

    public object PartOne(string input) => NumberAt(input, 2020);
    public object PartTwo(string input) => NumberAt(input, 30000000);

    public int NumberAt(string input, int count) {
        var numbers = input.Split(",").Select(int.Parse).ToArray();
        var (lastSeen, number) = (new int[count], numbers[0]);
        for (var round = 0; round < count; round++) {
            (lastSeen[number], number) = (round,
                round < numbers.Length ? numbers[round] :
                lastSeen[number] == 0  ? 0 :
                /* otherwise */          round - lastSeen[number]);
        }
        return number;
    }
}

Please ☆ my repo if you like it!

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