Repository navigation
alphametics: optimise implementation for last test - #935
Merged
Merged
Conversation
SleeplessByte
approved these changes
Jan 15, 2021
SleeplessByte
left a comment
Member
There was a problem hiding this comment.
Much better solution. Thank you @slaymance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR addresses Issue 897.
I refactored the example solution using my solution to alphametics as a guideline. The example implementation struggled a bit with the last test because we would replace each letter with a number every time we wanted to test a permutation. I refactored it so that we sum up the places of each letter and then multiply that sum by a number for each permutation, which is much faster than replacing each letter individually. For example:
For letters the appear in the total (right side of the equals sign), then their count is subtracted. At the end, we add up all the counts of the letters and check to see if the sum is equal to 0. If so, then that permutation of numbers is our solution.