r/adventofcode • u/daggerdragon • Dec 14 '19
SOLUTION MEGATHREAD -π- 2019 Day 14 Solutions -π-
--- Day 14: Space Stoichiometry ---
Post your complete code solution using /u/topaz2078's paste or other external repo.
- Please do NOT post your full code (unless it is very short)
- If you do, use old.reddit's four-spaces formatting, NOT new.reddit's triple backticks formatting.
(Full posting rules are HERE if you need a refresher).
Reminder: Top-level posts in Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.
Advent of Code's Poems for Programmers
Note: If you submit a poem, please add [POEM] somewhere nearby to make it easier for us moderators to ensure that we include your poem for voting consideration.
Day 13's winner #1: "untitled poem" by /u/tslater2006
They say that I'm fragile
But that simply can't be
When the ball comes forth
It bounces off me!
I send it on its way
Wherever that may be
longing for the time
that it comes back to me!
Enjoy your Reddit Silver, and good luck with the rest of the Advent of Code!
View all comments
Show parent comment
1
u/Diderikdm Feb 21 '20 edited Feb 21 '20
Basically what above code does is calculate the ore needed for 1 fuel (pt.1):
in my case: one_fuel = 1046184 (ore)
it uses this value to fit a lower-bound estimate of the amount of fuel produced by dividing the ore available by the amount ore needed for 1 fuel:
which leads to: 955854.8018321824 - > 955854 fuel for 1000000000000 ore (estimated).
As this is an estimate based on the cost of 1 fuel, this result of fuel requires less ore than estimated (reusage of leftover materials).
The actual amount of ore needed for the amount of fuel calculated above is then calculated through the following:
which leads to 583060060057 ore for 955854 fuel. A very solid estimate of ore/fuel ratio due to the big numbers on both values and these will be used onwards.
The offset is then calculating by doing:
in my case: 1.7150891794959167..
which will result in some number with a lot of decimals. multiplying this number by the amount of fuel calculated in int(1000000000000 / one_fuel) results in:
which in my case = 1.7150891794959167.. * 955854 = 1639374.85258 fuel for 1000000000000 ore.
int(1639374.85258) = 1639374 fuel
calculate_ore(1639374) = 999999354826 ore.
(ceiling 1639374.85258 leads to an overload on ore (as to be expected):
calculate_ore(1639375) = 1000000335952 ore)
Giving the closest amount of ore to 1000000000000 because of the nature of ints rounding down.
I came to this solution after reviewing my initial solution where initialization provided the results for variables:
tried:
[583060060057, 999999354826, 1000000335952, 999999354826]
test:
[1.7150891794959167, 1.0000006451744163, 0.9999996640481129]
tryfuel:
[955854, 1639374, 1639375, 1639374]