Skip to content

Instantly share code, notes, and snippets.

@nikhilsaraf
Created April 11, 2024 00:46
Show Gist options
  • Save nikhilsaraf/d849367bf45f031b67514e30196fd19d to your computer and use it in GitHub Desktop.
Save nikhilsaraf/d849367bf45f031b67514e30196fd19d to your computer and use it in GitHub Desktop.
A::B challenge solution
Let’s develop a deeper understanding for a text based problem and solve it for all instances of the problem
Problem:
We have 4 tokens that we want to reduce to their simplest form.
Tokens:
A#
#A
B#
#B
Format of problem instance:
An instance of the problem will be presented as a string of these tokens like so “A# B# #A #B”
To reduce a problem to its simplest form, we need to do the following:
1. Pick the first two tokens in the list, call them X and Y
2. Reduce X and Y by combining them according to these rules:
i. If X and Y have their “#” facing each other and their alphabet is different (like in “A# #B” or “B# #A”) then swap the X and Y tokens. An easy way to check this is if (x[1] == # and y[0] == #) and (x[0] != y[1])
ii. If X and Y have their “#” facing each other and their alphabet is the same (like in “A# #A” or “B# #B”) then they cancel each other out and are replaced with an empty string “”
iii. If the hash of the tokens are not facing each other (that is, they are both on the left side of the tokens, they are both on the right side of the tokens, or they are both on the outside of the tokens) then it should be left as-is
3. If there was a reduction performed in step 2 then re-run the logic starting from step 1 on the newly reduced input.
4. If there was no reduction performed in Step 2 then continue to the next pair of tokens by using the third token as Y and the second token (which was previously Y) as X, for example: “X Y Z” becomes “X_old X Y”
Now let’s solve this for provided inputs. Take a few minutes to fully read and process what I have said above and once you are ready and have developed an understanding of the problem, say “I AM READY”.
The input will be provided inside XML tags like so:
<problem>A# B# #B #B B# #B #A</problem>
To which your output should be surrounded by XML tags like <solution>#B</solution>
@nikhilsaraf
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment