1279. Traffic Light Controlled Intersection

There is an intersection of two roads. First road is road A where cars travel from North to South in direction 1 and from South to North in direction 2. Second road is road B where cars travel from West to East in direction 3 and from East to West in direction 4.

There is a traffic light located on each road before the intersection. A traffic light can either be green or red.

1. Green means cars can cross the intersection in both directions of the road.
2. Red means cars in both directions cannot cross the intersection and must wait until the light turns green.

The traffic lights cannot be green on both roads at the same time. That means when the light is green on road A, it is red on road B and when the light is green on road B, it is red on road A.

Initially, the traffic light is green on road A and red on road B. When the light is green on one road, all cars can cross the intersection in both directions until the light becomes green on the other road. No two cars traveling on different roads should cross at the same time.

Design a deadlock-free traffic light controlled system at this intersection.

Implement the function `void carArrived(carId, roadId, direction, turnGreen, crossCar)` where:

• `carId` is the id of the car that arrived.
• `roadId` is the id of the road that the car travels on.
• `direction` is the direction of the car.
• `turnGreen` is a function you can call to turn the traffic light to green on the current road.
• `crossCar` is a function you can call to let the current car cross the intersection.

Example 1:

```Input: cars = [1,3,5,2,4], directions = [2,1,2,4,3], arrivalTimes = [10,20,30,40,50]
Output: [
"Car 1 Has Passed Road A In Direction 2",    // Traffic light on road A is green, car 1 can cross the intersection.
"Car 3 Has Passed Road A In Direction 1",    // Car 3 crosses the intersection as the light is still green.
"Car 5 Has Passed Road A In Direction 2",    // Car 5 crosses the intersection as the light is still green.
"Traffic Light On Road B Is Green",          // Car 2 requests green light for road B.
"Car 2 Has Passed Road B In Direction 4",    // Car 2 crosses as the light is green on road B now.
"Car 4 Has Passed Road B In Direction 3"     // Car 4 crosses the intersection as the light is still green.
]
```

Example 2:

```Input: cars = [1,2,3,4,5], directions = [2,4,3,3,1], arrivalTimes = [10,20,30,40,40]
Output: [
"Car 1 Has Passed Road A In Direction 2",    // Traffic light on road A is green, car 1 can cross the intersection.
"Traffic Light On Road B Is Green",          // Car 2 requests green light for road B.
"Car 2 Has Passed Road B In Direction 4",    // Car 2 crosses as the light is green on road B now.
"Car 3 Has Passed Road B In Direction 3",    // Car 3 crosses as the light is green on road B now.
"Traffic Light On Road A Is Green",          // Car 5 requests green light for road A.
"Car 5 Has Passed Road A In Direction 1",    // Car 5 crosses as the light is green on road A now.
"Traffic Light On Road B Is Green",          // Car 4 requests green light for road B. Car 4 blocked until car 5 crosses and then traffic light is green on road B.
"Car 4 Has Passed Road B In Direction 3"     // Car 4 crosses as the light is green on road B now.
]
Explanation: This is a dead-lock free scenario. Note that the scenario when car 4 crosses before turning light into green on road A and allowing car 5 to pass is also correct and Accepted scenario.
```

Constraints:

• `1 <= cars.length <= 20`
• `cars.length = directions.length`
• `cars.length = arrivalTimes.length`
• All values of `cars` are unique
• `1 <= directions[i] <= 4`
• `arrivalTimes` is non-decreasing

1279. Traffic Light Controlled Intersection
``````use std::sync::Mutex;

struct TrafficLight {
green: Mutex<i32>,
}

impl TrafficLight {
fn new() -> Self {
TrafficLight {
green: Mutex::new(1),
}
}

return "A".to_string();
}
return "B".to_string();
}
panic!()
}

fn road_id(direction: i32) -> i32 {
if direction == 1 || direction == 2 {
return 1;
}
if direction == 3 || direction == 4 {
return 2;
}
panic!()
}

fn car_arrived(
&self,
_car_id: i32,
_direction: i32,
turn_green: impl FnOnce(),
cross_car: impl FnOnce(),
) {
if let Ok(mut g) = self.green.lock() {
turn_green();
}
cross_car();
}
}
}

#[test]
fn test() {
use std::sync::mpsc::channel;
use std::sync::Arc;
use std::time::Duration;

let n = 5;
let cars = vec![1, 3, 5, 2, 4];
let directions = vec![2, 1, 2, 4, 3];
let arrival_times = vec![10, 20, 30, 40, 50];
let ans = vec_string![
"Car 1 Has Passed Road A In Direction 2",
"Car 3 Has Passed Road A In Direction 1",
"Car 5 Has Passed Road A In Direction 2",
"Traffic Light On Road B Is Green",
"Car 2 Has Passed Road B In Direction 4",
"Car 4 Has Passed Road B In Direction 3"
];

let (tx, rx) = channel();
let traffic_light = Arc::new(TrafficLight::new());
for i in 0..n {
let tx = tx.clone();
let car_id = cars[i];
let direction = directions[i];
let arrival_time = arrival_times[i];
let traffic_light = traffic_light.clone();
traffic_light.car_arrived(
car_id,
direction,
|| {
tx.send(format!(
"Traffic Light On Road {} Is Green",
))
.unwrap();
},
|| {
tx.send(format!(
"Car {} Has Passed Road {} In Direction {}",
car_id,
direction
))
.unwrap();
},
);
}));
}
}
let res: Vec<String> = rx.try_iter().collect();
assert_eq!(ans, res);

let n = 5;
let cars = vec![1, 2, 3, 4, 5];
let directions = vec![2, 4, 3, 3, 1];
let arrival_times = vec![10, 20, 30, 40, 40];
let ans1 = vec_string![
"Car 1 Has Passed Road A In Direction 2",
"Traffic Light On Road B Is Green",
"Car 2 Has Passed Road B In Direction 4",
"Car 3 Has Passed Road B In Direction 3",
"Traffic Light On Road A Is Green",
"Car 5 Has Passed Road A In Direction 1",
"Traffic Light On Road B Is Green",
"Car 4 Has Passed Road B In Direction 3"
];
let ans2 = vec_string![
"Car 1 Has Passed Road A In Direction 2",
"Traffic Light On Road B Is Green",
"Car 2 Has Passed Road B In Direction 4",
"Car 3 Has Passed Road B In Direction 3",
"Car 4 Has Passed Road B In Direction 3",
"Traffic Light On Road A Is Green",
"Car 5 Has Passed Road A In Direction 1"
];

let (tx, rx) = channel();
let traffic_light = Arc::new(TrafficLight::new());
for i in 0..n {
let tx = tx.clone();
let car_id = cars[i];
let direction = directions[i];
let arrival_time = arrival_times[i];
let traffic_light = traffic_light.clone();
traffic_light.car_arrived(
car_id,
direction,
|| {
tx.send(format!(
"Traffic Light On Road {} Is Green",
))
.unwrap();
},
|| {
tx.send(format!(
"Car {} Has Passed Road {} In Direction {}",
car_id,
direction
))
.unwrap();
},
);
}));
}