From 1428bb14dce174b1a6c7fad14125be34fcd86a26 Mon Sep 17 00:00:00 2001 From: Thibaud Date: Tue, 3 Dec 2019 21:55:23 +0100 Subject: [PATCH] Day 1 challenge --- day1/day1.py | 41 ++++++++++++++++++++ day1/input.txt | 100 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 141 insertions(+) create mode 100644 day1/day1.py create mode 100644 day1/input.txt diff --git a/day1/day1.py b/day1/day1.py new file mode 100644 index 0000000..55009a3 --- /dev/null +++ b/day1/day1.py @@ -0,0 +1,41 @@ +#! /usr/bin/env python3 + + +def calculate_fuel_iterative(mass): + total = 0 + new_mass = mass + while True: + new_mass = new_mass // 3 - 2 + if new_mass < 0: + break + total += new_mass + return total + + +def calculate_total_fuel_mass(input_file, mass_function=lambda x: x // 3 - 2): + total = 0 + with open(input_file) as masses: + for mass in masses: + total += mass_function(int(mass)) + return total + + +def test_part2(): + inputs = (14, 1969, 100756) + expected = (2, 966, 50346) + for i, inp in enumerate(inputs): + result = calculate_fuel_iterative(inp) + assert result == expected[i], "Result for {} should be {}, was {}".format( + inp, expected[i], result + ) + print("All tests passed for part 2.") + + +if __name__ == "__main__": + print("Part 1 - total mass: ", calculate_total_fuel_mass("./input.txt")) + + test_part2() + print( + "Part 2 -- total mass: ", + calculate_total_fuel_mass("./input.txt", calculate_fuel_iterative), + ) diff --git a/day1/input.txt b/day1/input.txt new file mode 100644 index 0000000..ab07505 --- /dev/null +++ b/day1/input.txt @@ -0,0 +1,100 @@ +62259 +75368 +93740 +119724 +112546 +137714 +96999 +130673 +102398 +73819 +100734 +85337 +62764 +82115 +127696 +54391 +103213 +77954 +112513 +112392 +138404 +92989 +108521 +83163 +109720 +91918 +114443 +54306 +90623 +66833 +58505 +85919 +77539 +149419 +128385 +66452 +94677 +109179 +62072 +137245 +136226 +145783 +60689 +103320 +145931 +101286 +63458 +122468 +87858 +105675 +146185 +57417 +96883 +70739 +97494 +140951 +149416 +83137 +66122 +134319 +58511 +139600 +102929 +112240 +149634 +64142 +83332 +129526 +99058 +148889 +50087 +74961 +133606 +143518 +68849 +97045 +73920 +61357 +115941 +56740 +111773 +77880 +90792 +77103 +111355 +125898 +56547 +84918 +113822 +74113 +98557 +80928 +60519 +146379 +59354 +102490 +72584 +59000 +63151 +114253