Advent of Code 2019 馃く (讬讜诐 1)

讛讗 诇讻诐 住讬驻讜专 诪注谞讬讬谉: 讻诇 砖谞讛, 讻讞讜讚砖 诇驻谞讬 注专讘 讛砖谞讛 讛讗讝专讞讬转 讛讞讚砖讛 (袧芯胁褘泄 袚芯写, 住讬诇讘住讟专, 诪讛 砖转专爪讜), 诪转驻专住诐 诪砖讞拽 砖诪转讻谞转讬诐 讘专讞讘讬 讛注讜诇诐 讗讜讛讘讬诐 诪讗讜讚 讜谞拽专讗 Advent of Code.

讛拽讚诪讛

诇诪讬 砖诇讗 诪讻讬专 鈥 Advent of ... 讛讬讗 诪注讬谉 住驻讬专转 讬讬讗讜砖 谞讜爪专讬转 注讚 砖诪讙讬注讛 讛砖谞讛 讛讞讚砖讛. 讞诇拽 诪讛谞讜爪专讬诐 讞讜讙讙讬诐 讗转 讛住讬驻讜专 诪诪砖 注诐 诇讜讞 砖谞讛 砖诇 转拽讜驻转 讛爪讬驻讬讬讛 砖讻讜诇诇 住驻讬专讛 砖诇 24 注讚 31 讬诪讬诐 诇讗讞讜专, 讜讘讞诇拽 诪讛诇讜讞讜转 讛诇诇讜 讬砖 讞诇讜谞讜转 拽讟谞讬诐 砖诇 诪转谞讜转 住诪诇讬讜转, 砖讛讜诇讻讜转 讜谞讛讬讜转 诪砖诪注讜转讬讜转 讬讜转专 讻讻诇 砖诪讜注讚 讛砖谞讛 讛讞讚砖讛 诪转拽专讘.
讗讞讚 讛诪讬诪讜砖讬诐 讛讗讛讜讘讬诐 注诇讬, 诇讚讜讙诪讛, 讛讜讗 Rum Advent Calendar, 砖讘讜 讘讻诇 讞诇讜谉 砖讻讝讛 讬砖 讘拽讘讜拽讜谉 专讜诐. :stuck_out_tongue:

讬讗 讞讜驻专 诪讛 讛讗转讙专

诪讚讜讘专 讘讗转专 砖诇拽专讗转 讛砖谞讛 讛讞讚砖讛 诪驻专住诐 转专讙讬诇讬 拽讜讚 讞讘讬讘讬诐, 讗讞讚 讻诇 讬讜诐, 注讚 讛砖谞讛 讛讞讚砖讛.
讞砖讘转讬 砖讬讛讬讛 谞讞诪讚 诇讛转讞讬诇 诇驻转讜专 驻讛 讬讞讚 讗转讻诐 讗转 讛讗转讙专讬诐 砖讛讬讜 讘砖谞讛 讛讗讞专讜谞讛. 诪转讜拽祝 讛谞住讬讘讜转 讜诪注爪诐 讝讛 砖讻讜诇讻诐 注诪讜住讬诐, 谞讞讻讛 注讚 砖讬讛讬讛 诪住驻专 住讘讬专 砖诇 驻转专讜谞讜转 诇讗转讙专, 讜讗讞诇讬讟 诇驻讬 讝讛 诪转讬 注讜讘讚讬诐 注诇 讛讬讜诐 讛讘讗 砖驻讜专住诐 讘志Advent of Code.
讛讘讜谞讜住 讛谞讞诪讚 鈥 讗谞讬 讗驻专住诐 讙诐 讗转 讛驻转专讜谉 砖诇讬 诇讻诇 讗讞讚 诪讛转专讙讬诇讬诐.

讘讻诇 鈥溩欁曌濃 讘讗转讙专 讬砖 砖谞讬 转专讙讬诇讬诐 鈥 讛砖谞讬 谞驻转讞 专拽 讗讞专讬 砖转驻转专讜 讗转 讛专讗砖讜谉. 讘诪住讙专转 讛讗转讙专 讗谞讞谞讜 谞驻转讜专 讗转 砖谞讬讛诐.

讗讝 讘讜讗讜 谞转讞讬诇!
讛讬专砖诪讜 诇志Advent of Code 讜驻专住诪讜 驻讛 讗转 讛转砖讜讘讛 砖诇讻诐 诇砖讗诇讛 讛专讗砖讜谞讛.

8 诇讬讬拽讬诐

诇讬讬拽 1

讜讛谞讛 讞诇拽 2:

诇讬讬拽 1

7 诇讬讬拽讬诐

讗转讛 砖讟讜转谞讬拽 砖讝讛 诪砖讛讜. :slight_smile:
讗诐 转注讘讜讚 讗讬转讬 讬讜诐 讗讞讚 讜转讻转讜讘 砖诪讜转 诪砖转谞讬诐 讻讗诇讜 讗谞讬 诪讻谞讬住 讗讜转讱 诇诪拽专专 讜谞讜注诇 讗转 讛诪砖专讚
:heart:

8 诇讬讬拽讬诐

诪爪讟注专, senpi!
驻注诐 讗讞专讜谞讛 砖讗谞讬 砖诐 专讜讜讞 诇驻谞讬 住讜讙专讬讬诐 讘讛讙讚专转 驻讜谞拽爪讬讛! :heart:

(讗诐 讗转讛 谞讜注诇 讗讜转讬 讘诪拽专专, 讗谞讬 诪讞住诇 诇讱 讗转 讻诇 讛注讜讙讜转!
转诪讜转 谞驻砖讬 注诐 拽讬砖讟讬诐!!!)

4 诇讬讬拽讬诐

:star: :star: - 诪拽讜讜讛 砖住谞转讛 诪注专讬讱 讗转 讛诪讗诪抓 讞讞讞

讛拽讜讚 砖诇讬
def fuel_for_mass(mass):
    if mass <= 5:
        return 0
    mass = (mass // 3) - 2
    return fuel_for_mass(mass) + mass


def all_fuel_needed(all_masses):
    total_fuel = 0
    for star_mass in all_masses:
        mass_as_int = int(star_mass)
        total_fuel += fuel_for_mass(mass_as_int)
    return total_fuel


with open('resources/all_masses.txt', 'r') as file:
    all_masses = file.readlines()
all_fuel_needed(all_masses)
4 诇讬讬拽讬诐
讞诇拽 1
def get_correct_fuel_amount(mass):
    # Returns the fuel amount requiered from a specific mass
    fuel = (mass // 3) - 2
    return fuel


def get_fuel_requierments():
    # Returns the sum of all the fuel amounts
    total_amount = 0
    with open(r"resources\adventofcode\fuels amounts.txt", 'r') as file:
        for line in file.readlines():
            total_amount += get_correct_fuel_amount(int(line))
    return total_amount


print(get_fuel_requierments())
讞诇拽 2
def get_correct_fuel_amount_and_repeat(mass):
    # Returns the fuel amount requeired from a specific mass after calculating also the next calculations for the fuels
    fuel = (mass // 3) - 2
    if fuel <= 0:
        return 0
    return fuel + get_correct_fuel_amount_and_repeat(fuel)


def get_fuel_repeats_requierments():
    # Returns the sum of all the fuel amounts after all the calculations
    total_amount = 0
    with open(r"resources\adventofcode\fuels amounts.txt", 'r') as file:
        for line in file.readlines():
            total_amount += get_correct_fuel_amount_and_repeat(int(line))
    return total_amount


print(get_fuel_repeats_requierments())
2 诇讬讬拽讬诐

谞专讗讛 诇讬 砖讻诇讻讱 谞讛谞讬转 注诐 讛砖诪讜转 诪砖转谞讬诐 砖诇讗 砖诪转 诇讘 砖讻谞专讗讛 讬砖 诇讱 讟注讜转 讗讬驻砖讛讜. 讛驻诇讟 砖诇讱 拽专讜讘, 讗讘诇 诇讗 讛转砖讜讘讛 讛诪讚讜讬讬拽转 :sweat_smile:
诪注谞讬讬谉 讗讬驻讛 讛讟注讜转鈥 注诇 驻谞讬讜 谞专讗讛 住讘讘讛
注专讬讻讛: 谞专讗讛 砖讗转讛 爪讜讚拽! 诪爪讟注专 砖讛讟诇转讬 住驻拽 讘谞讻讜谞讜转 :innocent:

诇讬讬拽 1

fuel_1 讛讬讗 讛驻讜谞拽爪讬讛 砖诪讞砖讘转 诇驻讬 讛讘拽砖讛 讘住注讬祝 讗 , fuel_2 诇驻讬 讛讘拽砖讛 讘住注讬祝 讘鈥

讗讜转讜 讛讚讘专 讘讙讬专住讛 讟讬驻讛 讬讜转专 诪拽讜爪专转 :

诇讬讬拽 1

讗讬讝讛 讞专讬驻讬诐 讗转诐! 讻诇 讛讻讘讜讚 :slight_smile:

驻转专讜谉 砖讻转讘转讬 注讘讜专 讛驻讜专讜诐:

def get_inputs():
    with open('input.txt', 'r') as challenge_input:
        for line in challenge_input.readlines():
            yield int(line.strip())


def calculate_fuel(inputs):
    for fuel_input in inputs:
        yield fuel_input // 3 - 2


def calculate_total_fuel(fuel):
    if fuel <= 0:
        return 0
    fuel_needed = fuel // 3 - 2
    return fuel_needed + calculate_total_fuel(fuel_needed)


def get_total_fuels(inputs):
    for fuel in inputs:
        yield calculate_total_fuel(fuel)


print(sum(calculate_fuel(get_inputs())))
print(sum(get_total_fuels(get_inputs())))

驻转专讜谉 砖讻转讘转讬 诇注爪诪讬 (诪讬 砖专讜爪讛 诇讛专讞讬讘 讬讻讜诇; 注诇 讞诇拽 讙讚讜诇 谞讚讘专 讛砖讘讜注)

from typing import Generator

def get_input() -> Generator[str, None, None]:
    with open('input.txt', 'r') as challenge_input:
        yield from map(str.strip, challenge_input)


def calculate_total_fuel(fuel: int) -> int:
    if fuel <= 0:
        return 0
    fuel_needed = fuel // 3 - 2
    return fuel_needed + calculate_total_fuel(fuel_needed)


# Part 1
print(sum(fuel // 3 - 2 for fuel in map(int, get_input())))

# Part 2
print(sum(map(calculate_total_fuel, map(int, get_input()))))
7 诇讬讬拽讬诐

讗讛讘转讬 砖讗转讛 诪诪砖 诪诪砖 诪驻专讬讚 讛讻诇 诇驻讜谞拽爪讬讜转 鈥 讬砖 诇讬 注讜讚 诪讛 诇诇诪讜讚 讘谞讜砖讗 :slight_smile:
转讜讚讛.

诇讬讬拽 1

讛讝谞转讬 讗讜转讜, 讜讝讛 拽讚诐 讗讜转讬 讛诇讗讛.
(讬讻讜诇 诇讛讬讜转讛 砖诇讗 讻讜诇谞讜 拽讘诇谞讜 讗转 讗讜转诐 讛诪住驻专讬诐?)

诪讜讝专鈥 专讗讬转讬 讘注讜讚 驻转专讜谞讜转 砖驻专住诪讜 驻讛 砖讬讜爪讗 讻诪讜 讗爪诇讬 讗讝 讛谞讞转讬 砖诇讗.
注专讬讻讛: 诪住转讘专 砖讗转讛 爪讜讚拽! 注专讻转讬 讙诐 诇诪注诇讛 :grimacing:

:sunglasses: 讗讛讘转讬 诪诪砖 讗转 讛专注讬讜谉.
讝讛 讛驻转专讜谉 砖诇讬 诇转专讙讬诇 讛专讗砖讜谉 (讗讬讱 讗转诐 诪讚讘讬拽讬诐 讘驻讜专讜诐 拽讜讚? 讘诇讬 爪讬诇讜诐 诪住讱)

 import math


def calc_fuel_requierd(mass):
    mass = int(mass)
    fuel_requierd = math.floor(mass / 3) - 2
    return fuel_requierd


def get_masses_input():
    with open('resources/masses.txt', 'r') as masses_file:
        masses_list = masses_file.readlines()
    return masses_list


def sum_all_fuel_requierd(masses_list):
    sum_fuel_requiered = 0
    for mass in masses_list:
        sum_fuel_requiered = sum_fuel_requiered + calc_fuel_requierd(mass)
    return sum_fuel_requiered

masses_list = get_masses_input()
print(sum_all_fuel_requierd(masses_list))

and here is part two: :sunglasses:

import math

def calc_fuel_requierd(mass):
    mass = int(mass)
    fuel_requierd = math.floor(mass / 3) - 2
    return fuel_requierd


def get_masses_input():
    with open('resources/masses.txt', 'r') as masses_file:
        masses_list = masses_file.readlines()
    return masses_list


def sum_all_fuel_requierd(mass):
    sum_fuel_requiered = 0
    for mass in masses_list:
        temp_mass = int(mass)
        while temp_mass > 0:
            temp_mass = calc_fuel_requierd(temp_mass)
            if temp_mass > 0:
                sum_fuel_requiered = sum_fuel_requiered + temp_mass
    return sum_fuel_requiered

masses_list = get_masses_input()
print(sum_all_fuel_requierd(masses_list))

诇讬讬拽 1
4 诇讬讬拽讬诐
转砖讜讘讛
import math


with open('resources/input.txt', 'r') as fh:
    lines = fh.readlines()


def get_fuel_by_mass_part_1(input_mass):
    return math.floor(input_mass / 3) - 2


def get_fuel_by_mass_part_2(input_mass):
    fuel = get_fuel_by_mass_part_1(input_mass)
    if fuel <= 0:
        return 0
    return fuel + get_fuel_by_mass_part_2(fuel)


total_fuel = 0
for mass in lines:
    total_fuel += get_fuel_by_mass_part_2(int(mass.strip()))
print(total_fuel)

转砖讜讘讛 讞诇拽 1 = 3361299
转砖讜讘讛 讞诇拽 2 = 5039071

诇讬讬拽 1

讻砖谞讬住讬转讬 诇讛专讬抓 拽讬讘诇转讬 砖讙讬讗讛 砖 Generator 讗讬谞讜 诪讜讙讚专. 诪讛 爪专讬讱 诇讛讜住讬祝 讻讚讬 砖讝讛 讬专讜抓? (讙讜讙诇 注讚 讻讛 讛讘讬讗 诇讬 专拽 讚讘专讬诐 注诇 讙谞专讟讜专讬诐 砖讻讘专 诇诪讚谞讜)

讛讜住驻转讬 :slight_smile:

2 诇讬讬拽讬诐

讙诐 诇讬 谞专讗讛 砖讻讜诇谞讜 诇讗 诪拽讘诇讬诐 讗转 讗讜转诐 讛诪住驻专讬诐. 谞专讗讛 砖诇讻诇 讗讞讚 讬爪讗 诪住驻专 讗讞专 :stuck_out_tongue:

2 诇讬讬拽讬诐