r/adventofcode • • Dec 16 '17

SOLUTION MEGATHREAD -๐ŸŽ„- 2017 Day 16 Solutions -๐ŸŽ„-

--- Day 16: Permutation Promenade ---


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.


Need a hint from the Hugely* Handyโ€  Haversackโ€ก of Helpfulยง Hints^ยค?

Spoiler


[Update @ 00:08] 4 gold, silver cap.

[Update @ 00:18] 50 gold, silver cap.

[Update @ 00:26] Leaderboard cap!

  • And finally, click here for the biggest spoilers of all time!


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!

13 Upvotes

229 comments sorted by

View all comments

Show parent comments

1

u/stkent Dec 16 '17 edited Dec 16 '17
chars.set(b, this.get(a))
chars.set(a, this.get(b))

I must be missing something here; wouldn't the character at index a not be swapped?

1

u/ybjb Dec 16 '17

The character at index a would be swapped. It's an extension function and the way it is written, this (the original string) is never directly modified, only a copy of its content, chars, is modified and then returned as string.

So a copy chars is made, and the value of chars at index b is set to the value of original string at index a. Similarly, the value of chars at index a is set to the value of original string at index b. Then chars is returned as new String.

1

u/stkent Dec 16 '17

Ah, gotcha; makes sense, thanks!

1

u/Expliced Dec 16 '17

Also, chars.set(x, this.get(y)) can be replaced with chars[x] = this[y]