01020304050607080910111213141516171819202122232425

Advent of Code

2019/1

The Tyranny of the Rocket Equation

in C#

by encse

Santa has become stranded at the edge of the Solar System while delivering presents to other planets! To accurately calculate his position in space, safely align his warp drive, and return to Earth in time to save Christmas, he needs you to bring him measurements from fifty stars.

Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck!

Visit the website for the full story and full puzzle description.

using System;
using System.Collections.Generic;
using System.Linq;

namespace AdventOfCode.Y2019.Day01;

[ProblemName("The Tyranny of the Rocket Equation")]
class Solution : Solver {

    public object PartOne(string input) => Solve(input, false);
    public object PartTwo(string input) => Solve(input, true);

    int Solve(string input, bool recursive) {
        var weights = new Queue<int>(input.Split("\n").Select(x => int.Parse(x)));
        var res = 0;
        while (weights.Any()) {
            var weight = weights.Dequeue();
            var fuel = (int)(Math.Floor(weight / 3.0) - 2);
            if (fuel > 0) {
                if (recursive) {
                    weights.Enqueue(fuel);
                }
                res += fuel;
            }
        }
        return res;
    }
}

Please ☆ my repo if you like it!

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