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
2
u/AlphaDart1337 Dec 14 '19 edited Dec 14 '19
Correct me if I'm wrong, but it seems to me like this solution won't always work, because you're not controlling the order in which you process your "required" dictionary.
For example, let's look at the first sample from today's text. You start with required as A : 7, E : 1, which might evolve into A : 14, D : 1, then into A : 21, C : 1, then A : 28, B : 1, then ORE : 30, B : 1 and finally ORE : 31.
But A : 7, E : 1, if you start with A instead of E, could also evolve into ORE : 10, E : 1, which then evolves into ORE : 10, A : 7, D : 1, which could go into ORE : 20, D : 1, into ORE : 20, A : 7, C : 1, into ORE 30, C : 1, then ORE : 30, A : 7, B : 1, then ORE : 40, B : 1, and finally ORE : 41.
So it seems to me like the code only works because it luckily happens to iterate through the dictionary in the right order.
I think the correct solution is to first sort the ingredient graph topologically, and then process the nodes in topological order, to avoid any issues. But none of the solutions I've looked at thus far do that. Am I missing something?
EDIT: Nevermind, I misread the solution. I didn't realize ingredient amounts could become negative. Turns out I was indeed missing something :D. Sorry.