Skip to main content
Add Python code
Source Link
John Kemeny
  • 17.1k
  • 3
  • 43
  • 67

If you number each plank from $1$ to $n$, you can keep track of which plank each sensor last observed by keeping two counters $L_c$ and $R_c$ (for left counter and right counter, respectively).

When the left sensor, $L$ turns on, you increment $L_c$ (because $L$ is now looking at a new plank). When $L$ turns off, you increment $n$, the total number of planks observed.

When the right sensor, $R$ turns on, you increment $R_c$, and check if $L_c = R_c > n$. If it is so, then you increment $n_{\text{long}}$.

You can now compute $n_{\text{short}} = n - n_{\text{long}}$.

The reason it is correct is because $X_c$ is how many times sensor $X$ has been turned on, and indicates which plank it last saw. If, in addition, $L_c = R_c > n$ it means that the last plank they witnessed was plank $L_c$ and since $n$ hasn't been incremented yet, $L$ is currently observing it, hence $R$ and $L$ are observing the same plank, hence it is a long plank.


In code:

from dataclasses import dataclass

@dataclass
class Conveyor:
    L_c: int = 0
    R_c: int = 0
    n: int = 0
    n_long: int = 0

    def signal(self, sensor, status):
        if sensor == "L":
            if status == "ON":
                self.L_c += 1
            else:
                self.n += 1
        else:
            if status == "ON":
                self.R_c += 1
                if self.L_c == self.R_c > self.n:
                    self.n_long += 1

    @property
    def short(self):
        return self.n - self.n_long

    @property
    def long(self):
        return self.n_long
conveyor = Conveyor()
conveyor.signal("L", "ON")
conveyor.signal("L", "OFF")
conveyor.signal("R", "ON")
conveyor.signal("R", "OFF")
conveyor.signal("L", "ON")
conveyor.signal("R", "ON")
conveyor.signal("L", "OFF")
conveyor.signal("R", "OFF")
print(conveyor.short)
print(conveyor.long)

If you number each plank from $1$ to $n$, you can keep track of which plank each sensor last observed by keeping two counters $L_c$ and $R_c$ (for left counter and right counter, respectively).

When the left sensor, $L$ turns on, you increment $L_c$ (because $L$ is now looking at a new plank). When $L$ turns off, you increment $n$, the total number of planks observed.

When the right sensor, $R$ turns on, you increment $R_c$, and check if $L_c = R_c > n$. If it is so, then you increment $n_{\text{long}}$.

You can now compute $n_{\text{short}} = n - n_{\text{long}}$.

The reason it is correct is because $X_c$ is how many times sensor $X$ has been turned on, and indicates which plank it last saw. If, in addition, $L_c = R_c > n$ it means that the last plank they witnessed was plank $L_c$ and since $n$ hasn't been incremented yet, $L$ is currently observing it, hence $R$ and $L$ are observing the same plank, hence it is a long plank.

If you number each plank from $1$ to $n$, you can keep track of which plank each sensor last observed by keeping two counters $L_c$ and $R_c$ (for left counter and right counter, respectively).

When the left sensor, $L$ turns on, you increment $L_c$ (because $L$ is now looking at a new plank). When $L$ turns off, you increment $n$, the total number of planks observed.

When the right sensor, $R$ turns on, you increment $R_c$, and check if $L_c = R_c > n$. If it is so, then you increment $n_{\text{long}}$.

You can now compute $n_{\text{short}} = n - n_{\text{long}}$.

The reason it is correct is because $X_c$ is how many times sensor $X$ has been turned on, and indicates which plank it last saw. If, in addition, $L_c = R_c > n$ it means that the last plank they witnessed was plank $L_c$ and since $n$ hasn't been incremented yet, $L$ is currently observing it, hence $R$ and $L$ are observing the same plank, hence it is a long plank.


In code:

from dataclasses import dataclass

@dataclass
class Conveyor:
    L_c: int = 0
    R_c: int = 0
    n: int = 0
    n_long: int = 0

    def signal(self, sensor, status):
        if sensor == "L":
            if status == "ON":
                self.L_c += 1
            else:
                self.n += 1
        else:
            if status == "ON":
                self.R_c += 1
                if self.L_c == self.R_c > self.n:
                    self.n_long += 1

    @property
    def short(self):
        return self.n - self.n_long

    @property
    def long(self):
        return self.n_long
conveyor = Conveyor()
conveyor.signal("L", "ON")
conveyor.signal("L", "OFF")
conveyor.signal("R", "ON")
conveyor.signal("R", "OFF")
conveyor.signal("L", "ON")
conveyor.signal("R", "ON")
conveyor.signal("L", "OFF")
conveyor.signal("R", "OFF")
print(conveyor.short)
print(conveyor.long)
Source Link
John Kemeny
  • 17.1k
  • 3
  • 43
  • 67

If you number each plank from $1$ to $n$, you can keep track of which plank each sensor last observed by keeping two counters $L_c$ and $R_c$ (for left counter and right counter, respectively).

When the left sensor, $L$ turns on, you increment $L_c$ (because $L$ is now looking at a new plank). When $L$ turns off, you increment $n$, the total number of planks observed.

When the right sensor, $R$ turns on, you increment $R_c$, and check if $L_c = R_c > n$. If it is so, then you increment $n_{\text{long}}$.

You can now compute $n_{\text{short}} = n - n_{\text{long}}$.

The reason it is correct is because $X_c$ is how many times sensor $X$ has been turned on, and indicates which plank it last saw. If, in addition, $L_c = R_c > n$ it means that the last plank they witnessed was plank $L_c$ and since $n$ hasn't been incremented yet, $L$ is currently observing it, hence $R$ and $L$ are observing the same plank, hence it is a long plank.