2019 - Day 1 (The Tyranny of the Rocket Equation)

View puzzle description on AdventOfCode.com
Part 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/usr/bin/env python3

#Advent of Code
#2019 Day 1, Part 1
#Solution by James C. (https://github.com/JamesMCo)

import os, sys
sys.path.append(os.path.abspath("../.."))
import unittest, util.read
from util.tests import run

def solve(puzzle_input):
    return sum(int(x/3) - 2 for x in puzzle_input)

def main():
    puzzle_input = util.read.as_int_list("\n")

    fuel = solve(puzzle_input)

    print("The sum of the fuel requirements is " + str(fuel) + ".")

class AOC_Tests(unittest.TestCase):
    def test_ex1(self):
        self.assertEqual(solve([12]), 2)

    def test_ex2(self):
        self.assertEqual(solve([14]), 2)

    def test_ex3(self):
        self.assertEqual(solve([1969]), 654)

    def test_ex4(self):
        self.assertEqual(solve([100756]), 33583)

if __name__ == "__main__":
    run(main)
Part 2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/usr/bin/env python3

#Advent of Code
#2019 Day 1, Part 2
#Solution by James C. (https://github.com/JamesMCo)

import os, sys
sys.path.append(os.path.abspath("../.."))
import unittest, util.read
from util.tests import run

def solve(puzzle_input):
    def aux(n):
        s = int(n/3) - 2
        if s <= 0:
            return 0
        return s + aux(s)

    return sum(aux(x) for x in puzzle_input)

def main():
    puzzle_input = util.read.as_int_list("\n")

    fuel = solve(puzzle_input)

    print("The sum of the fuel requirements is " + str(fuel) + ".")

class AOC_Tests(unittest.TestCase):
    def test_ex1(self):
        self.assertEqual(solve([14]), 2)

    def test_ex2(self):
        self.assertEqual(solve([1969]), 966)

    def test_ex3(self):
        self.assertEqual(solve([100756]), 50346)

if __name__ == "__main__":
    run(main)
Return to solution index