בוודאות כל אחד מקבל מספרים אחרים זה כדי שלא יהיו העתקות חחחח
זה הקוד שלי, יוזמה נהדרת!
# ADVENT OF CODE: DAY 1, Puzzle 1
import math
def get_mass_inputs():
with open('input.txt', 'r') as file:
mass_list = file.read().splitlines()
for mass in mass_list:
yield int(mass)
def calc_fuel_for_mass(mass):
fuel = math.floor(mass / 3) - 2
return fuel
def calc_total_fuel_for_mass(mass):
fuel = math.floor(mass / 3) - 2
if fuel <= 0:
return 0
return fuel + calc_total_fuel_for_mass(fuel)
def get_total_fuel():
for mass in get_mass_inputs():
yield calc_total_fuel_for_mass(mass)
total_fuel = sum(get_total_fuel())
print(f"Total fuel needed for all modules: {total_fuel}")
חלק 1
def calculate_required_fuel(path):
with open(path, "r") as f:
module_masses = f.read().split()
fuel = 0
for module_mass in module_masses:
fuel += int(module_mass) // 3 - 2
return fuel
print(calculate_required_fuel("save-santa-day1-p1.txt"))
חלק 2
def calculate_required_fuel(path):
with open(path, "r") as f:
module_masses = f.read().split()
fuel = 0
for module_mass in module_masses:
base_module_fuel = int(module_mass) // 3 - 2
fuel += calculate_total_fuel(base_module_fuel)
return fuel
def calculate_total_fuel(required_fuel):
total_fuel = required_fuel
result = required_fuel // 3 - 2
while result > 0:
total_fuel += result
result = result // 3 - 2
return total_fuel
print(calculate_required_fuel("save-santa-day1-p1.txt"))
אני רואה שהרבה מכם משתמשים ב־math.floor
אחרי חילוק – זכרו שיש את האופרטור //
- מרפרפת על פתרונות
- רואה שחניך חוצפן קרא לפונקציה בשם גס
- רואה שהשם של הפונקציה הבאה ממש טיפשי
- רואה שלכל משתנה יש שם יותר דבילי מהשני
אה, זה גיא טריסקר.
הוא, כבר היה צריך להרחיק אותו בשבוע הראשון.
#רקאומר
חס וחלילה! אם לוקחים לי אותו אני מתפטרת #שומריםעלגיא
ים, יש טעות קלה בפתרון עבור הפורום
הקוד שלי, היה כיף
def fuel_for_module(mass):
return int(mass) // 3 - 2
def get_mass_input_transform_to_fuel(mass_file):
mass_to_fuel = []
with open(mass_file, "r") as all_mass_file:
all_mass_read = all_mass_file.readlines()
for mass in all_mass_read:
mass_to_fuel.append(fuel_for_module(mass))
return mass_to_fuel
def get_mass_input_transform_to_fuel_part2(mass_file):
mass_to_fuel = []
with open(mass_file, "r") as all_mass_file:
all_mass_read = all_mass_file.readlines()
for mass in all_mass_read:
fuel = 0
while int(mass) >= 9:
fuel += fuel_for_module(mass)
mass = fuel_for_module(mass)
mass_to_fuel.append(fuel)
return mass_to_fuel
print(sum(get_mass_input_transform_to_fuel("resources//Advent of code 2019_day1.txt")))
print(sum(get_mass_input_transform_to_fuel_part2("resources//Advent of code 2019_day1.txt")))
היי ים, יכול להיות שיש טעות קטנה בקוד שלך?
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)
בשורה של fuel קטן שווה לאפס זה לא צריך להיות קטן שווה ל 8?
(כי הערך 9 זה הערך שממנו ואילך תוצאת החישוב תצא מספר הגדול מאפס)
החלק הראשון
def calculate_each_amount(amount):
calculate = int(amount) // 3 - 2
return calculate
with open('etgar/etgar1.txt', 'r') as fuel_file:
amounts = fuel_file.readlines()
total = 0
for amount in amounts:
calculate = calculate_each_amount(amount)
total += calculate
print(total)
החלק השני
def calculate_each_amount(amount):
calculate = int(amount) // 3 - 2
if calculate <= 0:
return 0
else:
calculate += calculate_each_amount(calculate)
return calculate
with open('etgar/etgar1.txt', 'r') as fuel_file:
amounts = fuel_file.readlines()
total = 0
for amount in amounts:
calculate = calculate_each_amount(amount)
total += calculate
print(total)
לגמרי סביר שיש טעות קטנה בקוד שלי
סחטיין על העירנות.
מציין לטובה את @orronai ששלח לי על זה הודעה פרטית ממש כמה דקות אחרי שפרסמתי
אפשר גם להכניס קוד מתוך האתר repl
זה מאפשר גם לערוך אותו ולהריץ אותו.
למשל (פתרון חלק 2 של היום השני) :
https://repl.it/@yosefcohen1/GlisteningEnergeticHarddrives
import math
def mass_to_liters(mass):
if mass == "":
return 0
return math.floor(int(mass) / 3) - 2
def liters_for_all_masss(file):
tot_liters = []
for mass in file:
tot_liters.append(mass_to_liters(mass))
return sum(tot_liters)
with open(r'C:\Users\limor pelled\Documents\Idan Pelled\pythonic\daily challenge\resorses\fuel.txt', 'r') as fh:
file = fh.read()
החלק השני:
(החלק הראשון זהה, למעט הלולאת while)
def clean_lines(txt):
new_list = []
for line in txt:
new_list.append(int(line.strip('\n')))
return new_list
def calculator(num_list):
total_fuel = 0
for num in num_list:
fuel = 0
while num > 5:
num = num // 3 - 2
fuel += num
total_fuel += fuel
return total_fuel
with open('day1.txt', 'r') as file:
numbers = file.readlines()
calculator(clean_lines(numbers))
האם זה גם חובה להגשה?
לא, את התרגילים שתחת הקטגוריה “אתגר” לא חייבים להגיש. זה תרגול נחמד למי שרוצה (ומומלץ לכולם כמובן).
שים לב שבתיאור הקטגוריה כתוב במפורש: “בהמשך נפרסם כאן שאלות נוספות ושאלות אתגר למי שמעוניין להעמיק בחומר ולתרגל שאלות נוספות.”
# Part I
FUEL_DATA = 'Advent_Code/input.txt'
def calc_fuel(module_mass):
return module_mass // 3 - 2
def calc_modules_fuel(input_path):
with open(input_path, 'r') as mass_file:
mass_data = mass_file.readlines()
total_fuel = 0
for mass in mass_data:
total_fuel += calc_fuel(int(mass))
return total_fuel
print(calc_modules_fuel(FUEL_DATA))
# Part II
def module_total_fuel(module_mass):
start_fuel = calc_fuel(module_mass)
total_fuel = start_fuel
while calc_fuel(start_fuel) > 0:
total_fuel += calc_fuel(start_fuel)
start_fuel = calc_fuel(start_fuel)
return total_fuel
print(calc_modules_fuel(FUEL_DATA))
פונקציית החישוב הכללי נותרת ללא שינוי בחלק השני, אז מטעמי חיסכון לא צירפתי.
טוב קצת באיחור אבל הנה: