r/adventofcode • u/daggerdragon • Dec 05 '18
SOLUTION MEGATHREAD -🎄- 2018 Day 5 Solutions -🎄-
--- Day 5: Alchemical Reduction ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).
Note: The 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: The Party Game!
Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!
Card prompt: Day 5
Transcript:
On the fifth day of AoC / My true love sent to me / Five golden ___
This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.
edit: Leaderboard capped, thread unlocked at 0:10:20!
31
Upvotes
View all comments
Show parent comment
1
u/Hashbrown777 Dec 05 '18 edited Dec 05 '18
It's to declare another variable
charYou'll also notice in the check condition I don't just check the bounds of
indexbut I assigncharwith(char = input[index]).Basically just saves two lines (at the expense of making a stupid long one). I feel this still makes sense to read though, basically a low-level way of emulating an iterator for the string without setting up a real one see last 2 lines of code
If it gets any more custom I'll break it out into multiple lines anyway, like for my day1 solution which had an extra break-out condition.
Edit: Looking at my own reference there, I could've done away with the whole thing and just had
for (let char of input) {. You only need to do what I did if you also want the index, which for this we didn't. shrugs no idea if this would perform worse though (I'd imagine built-in iterators would be pretty fast, though), it certainly looks sexier.I mean, I didn't even need the
index < input.lengthcheck,input[index = input.length]would've safely returnedundefinedand been falsy enough to break the loop for me without explicitly checking. I was just too busy trying to get the solution really quick-likeBut I don't edit the code once I solve it for these, so the mess stays messy here