Skip to content

mohammadhasanii/binary-sync-go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 

Repository files navigation

binary-sync-go

A simple Go program to solve a puzzle about equalizing two binary strings by flipping bits in pairs.


The Challenge

Imagine you have two strings of lights, represented by 1 (ON 🟡) and 0 (OFF ⚫️).

Example:

String A: 10110  (🟡⚫️🟡🟡⚫️)
String B: 00000  (⚫️⚫️⚫️⚫️⚫️)

Your goal is to make String A identical to String B.

The Rule

In a single move, you can flip the state of exactly two lights. These two lights can be chosen from anywhere, in either string.

Find the minimum number of moves required. If it's impossible, the answer is "NO".


The Core Idea

The solution doesn't care about which specific lamps to flip or in what order. The entire logic hinges on a simple, fundamental property: parity. Since every move affects two lamps, it effectively fixes two differences at a time. This means if the total number of differences between the two strings is an odd number, the puzzle is impossible to solve. If it's an even number, the minimum number of moves is simply half the total number of differences.


My Journey & Takeaway

Honestly, for the first hour of looking at this problem, I was completely stuck. My mind was racing with complexity. I kept thinking, "Do I need to swap the lamps? Should I change them with their neighbors? Is there a complex pattern I'm missing?" I was trying to build a huge, complicated machine to solve what I thought was a huge, complicated problem.

Then, the realization hit me. The problem wasn't about the complex 'how'. It was about a simple, fundamental property.

I was shocked when I finally understood that the entire puzzle boiled down to one simple question: Is the total number of differences an even number? That's it. All that complexity I had imagined just melted away.

This was a powerful lesson: Don't build up problems to be monsters in your head. Dive in, challenge your assumptions, and search for the core logic. Often, the solution is far simpler and more elegant than you first imagine.


Fact

The logic of this problem is based on parity (evenness or oddness), a fundamental concept in computer science. The simplest error-checking mechanism in data transmission, the parity bit, works on a similar principle. A single bit is added to a binary string to ensure that the total number of 1s is always even (or always odd, depending on the system). If a single bit flips during transmission, the receiver can immediately detect that an error has occurred because the parity of the string will have changed!

About

A Go solution for a puzzle about synchronizing two binary strings by flipping characters in pairs.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages