using System.Collections.Generic;
public enum TrafficLightColor {
public class Intersection {
public string Name { get; set; }
public int[] SensorData { get; set; }
public bool EmergencyVehicleDetected { get; set; }
public int PedestrianCount { get; set; }
public Intersection(string name, int laneCount) {
SensorData = new int[laneCount];
public class TrafficLight {
public TrafficLightColor CurrentColor { get; set; }
public int GreenDuration { get; set; }
public class SmartTrafficSystem
private Dictionary<string, TrafficLight> trafficLights = new Dictionary<string, TrafficLight>();
private Dictionary<string, Intersection> intersections = new Dictionary<string, Intersection>();
private const int DefaultGreenDuration = 30;
private const int MaxGreenDuration = 60;
private const int MinGreenDuration = 10;
public void AddIntersection(Intersection intersection, int numberOfLanes)
intersections[intersection.Name] = intersection;
trafficLights[intersection.Name + "_Lane1"] = new TrafficLight { CurrentColor = TrafficLightColor.Red, GreenDuration = DefaultGreenDuration };
public void UpdateIntersectionData(string intersectionName, int[] sensorData, bool emergencyVehicleDetected, int pedestrianCount)
if (intersections.ContainsKey(intersectionName))
intersections[intersectionName].SensorData = sensorData;
intersections[intersectionName].EmergencyVehicleDetected = emergencyVehicleDetected;
intersections[intersectionName].PedestrianCount = pedestrianCount;
public TrafficLightColor GetTrafficLightState(string intersectionName)
if(trafficLights.TryGetValue(intersectionName + "_Lane1", out var light))
return light.CurrentColor;
return TrafficLightColor.Red;
public int GetTrafficLightGreenDuration(string intersectionName)
if (trafficLights.TryGetValue(intersectionName + "_Lane1", out var light))
return light.GreenDuration;
return DefaultGreenDuration;
public void AdjustTrafficLights()
foreach (var intersectionPair in intersections)
string intersectionName = intersectionPair.Key;
Intersection intersection = intersectionPair.Value;
if (intersection.EmergencyVehicleDetected)
trafficLights[intersectionName + "_Lane1"].CurrentColor = TrafficLightColor.Green;
trafficLights[intersectionName + "_Lane1"].GreenDuration = MaxGreenDuration;
foreach (int density in intersection.SensorData)
int baseDuration = DefaultGreenDuration;
baseDuration += intersection.PedestrianCount / 10;
baseDuration = Math.Min(baseDuration, MaxGreenDuration);
baseDuration = Math.Min(baseDuration, MaxGreenDuration);
trafficLights[intersectionName + "_Lane1"].GreenDuration = baseDuration;
trafficLights[intersectionName + "_Lane1"].CurrentColor = TrafficLightColor.Green;
public class SmartTrafficSystemTests
public void EmergencyVehiclePrioritized()
SmartTrafficSystem trafficSystem = new SmartTrafficSystem();
Intersection intersection = new Intersection("TestIntersection", 2);
trafficSystem.AddIntersection(intersection, 2);
trafficSystem.UpdateIntersectionData("TestIntersection", new int[] { 5, 5 }, true, 0);
trafficSystem.AdjustTrafficLights();
Assert.That(trafficSystem.GetTrafficLightState("TestIntersection"), Is.EqualTo(TrafficLightColor.Green));
Assert.That(trafficSystem.GetTrafficLightGreenDuration("TestIntersection"), Is.EqualTo(60));
public void HighPedestrianTrafficIncreasesCrossingTime()
SmartTrafficSystem trafficSystem = new SmartTrafficSystem();
Intersection intersection = new Intersection("TestIntersection", 2);
trafficSystem.AddIntersection(intersection, 2);
trafficSystem.UpdateIntersectionData("TestIntersection", new int[] { 5, 5 }, false, 50);
trafficSystem.AdjustTrafficLights();
Assert.That(trafficSystem.GetTrafficLightGreenDuration("TestIntersection"), Is.EqualTo(35));
public void StandardTrafficFlowAdjustment()
SmartTrafficSystem trafficSystem = new SmartTrafficSystem();
Intersection intersection = new Intersection("TestIntersection", 2);
trafficSystem.AddIntersection(intersection, 2);
trafficSystem.UpdateIntersectionData("TestIntersection", new int[] { 30, 30 }, false, 0);
trafficSystem.AdjustTrafficLights();
Assert.That(trafficSystem.GetTrafficLightState("TestIntersection"), Is.EqualTo(TrafficLightColor.Green));
Assert.That(trafficSystem.GetTrafficLightGreenDuration("TestIntersection"), Is.EqualTo(30));
trafficSystem.UpdateIntersectionData("TestIntersection", new int[] { 60, 60 }, false, 0);
trafficSystem.AdjustTrafficLights();
Assert.That(trafficSystem.GetTrafficLightGreenDuration("TestIntersection"), Is.EqualTo(40));
public void AddIntersection_AddsIntersectionAndTrafficLight()
SmartTrafficSystem trafficSystem = new SmartTrafficSystem();
Intersection intersection = new Intersection("Intersection1", 4);
trafficSystem.AddIntersection(intersection, 4);
Assert.That(trafficSystem.GetTrafficLightState("Intersection1"), Is.EqualTo(TrafficLightColor.Red));
Assert.That(trafficSystem.GetTrafficLightGreenDuration("Intersection1"), Is.EqualTo(30));
public void UpdateIntersectionData_UpdatesDataCorrectly()
SmartTrafficSystem trafficSystem = new SmartTrafficSystem();
Intersection intersection = new Intersection("Intersection1", 4);
trafficSystem.AddIntersection(intersection, 4);
int[] newSensorData = { 10, 20, 30, 40 };
bool newEmergencyVehicleDetected = true;
int newPedestrianCount = 15;
trafficSystem.UpdateIntersectionData("Intersection1", newSensorData, newEmergencyVehicleDetected, newPedestrianCount);
trafficSystem.AdjustTrafficLights();
Assert.That(trafficSystem.GetTrafficLightState("Intersection1"), Is.EqualTo(TrafficLightColor.Green));
Assert.That(trafficSystem.GetTrafficLightGreenDuration("Intersection1"), Is.EqualTo(60));
public void AdjustTrafficLights_SetsGreenLightAndCorrectDuration()
SmartTrafficSystem trafficSystem = new SmartTrafficSystem();
Intersection intersection = new Intersection("Intersection1", 4);
trafficSystem.AddIntersection(intersection, 4);
int[] newSensorData = { 10, 20, 30, 40 };
bool newEmergencyVehicleDetected = false;
int newPedestrianCount = 15;
trafficSystem.UpdateIntersectionData("Intersection1", newSensorData, newEmergencyVehicleDetected, newPedestrianCount);
trafficSystem.AdjustTrafficLights();
Assert.That(trafficSystem.GetTrafficLightState("Intersection1"), Is.EqualTo(TrafficLightColor.Green));
Assert.That(trafficSystem.GetTrafficLightGreenDuration("Intersection1"), Is.EqualTo(31));
public static class Program
public static void Main(string[] args)
new NUnitLite.AutoRun().Execute(["--noc" ]);