Skip to content

Comments

Implement dominoes push simulation#5

Merged
Subhosjx merged 1 commit intoSubhosjx:mainfrom
SjxSubham:main
Oct 27, 2025
Merged

Implement dominoes push simulation#5
Subhosjx merged 1 commit intoSubhosjx:mainfrom
SjxSubham:main

Conversation

@SjxSubham
Copy link
Contributor

Intuition

Imagine each domino can be affected by forces coming from the left and right:
• 'R' pushes a positive (rightward) force to its right.
• 'L' pushes a negative (leftward) force to its left.
• '.' just receives forces from both sides and falls accordingly.
Over time, forces from 'R' and 'L' spread outward but weaken (because distance reduces influence).
So we can think of this as:
Each domino experiences a net force = (right force) + (left force).
• If net force > 0 → falls to the right ('R')
• If net force < 0 → falls to the left ('L')
• If net force = 0 → remains upright ('.')

Approach

  1. Create an array forces of size n initialized with 0.
    • It will store the net force experienced by each domino.
  2. Left-to-right pass (for 'R'):
    • Keep a running force variable.
    • If you meet 'R', set force = n (max push).
    • If you meet 'L', reset force = 0.
    • For each '.', force = max(force - 1, 0) (force weakens by 1).
    • Add this force to forces[i].
  3. Right-to-left pass (for 'L'):
    • Same logic but reversed.
    • If you meet 'L', set force = -n (negative force).
    • If you meet 'R', reset force = 0.
    • For '.', force = min(force + 1, 0) (force weakens by 1).
    • Add this force to forces[i] (now we combine both left and right effects).
  4. Final result:
    For each i:
    • if forces[i] > 0 → 'R'
    • if forces[i] < 0 → 'L'
    • if forces[i] == 0 → '.'

@Subhosjx Subhosjx added the hacktoberfest-accepted hacktoberfest-accepted label Oct 27, 2025
@Subhosjx Subhosjx merged commit f30a5e7 into Subhosjx:main Oct 27, 2025
1 check passed
@github-actions
Copy link

🎉 Congrats on getting your PR merged in, @SjxSubham! 🙌🏼

Thanks for your contribution every effort helps improve the project.

Looking forward to seeing more from you! 🥳✨

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

Labels

hacktoberfest-accepted hacktoberfest-accepted

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants