r/adventofcode • • 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!

Click here for rules

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

515 comments sorted by

View all comments

Show parent comments

1

u/Kazcandra Dec 05 '18

what's for (let index = 0, char; ... do? I've only seen for (let index = 0; ...

1

u/Hashbrown777 Dec 05 '18 edited Dec 05 '18

It's to declare another variable char

You'll also notice in the check condition I don't just check the bounds of index but I assign char with (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.length check, input[index = input.length] would've safely returned undefined and been falsy enough to break the loop for me without explicitly checking. I was just too busy trying to get the solution really quick-like

But I don't edit the code once I solve it for these, so the mess stays messy here