|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'bundler/inline' |
| 4 | + |
| 5 | +gemfile do |
| 6 | + source 'https://rubygems.org' |
| 7 | + |
| 8 | + gem 'minitest' |
| 9 | +end |
| 10 | + |
| 11 | +class TrafficLightSystem |
| 12 | + def initialize |
| 13 | + @lights = [ |
| 14 | + { direction: "North-South", state: "red", timer: 10 }, |
| 15 | + { direction: "East-West", state: "green", timer: 8 } |
| 16 | + ] |
| 17 | + @pedestrian_signals = { |
| 18 | + "North-South" => false, |
| 19 | + "East-West" => false |
| 20 | + } |
| 21 | + end |
| 22 | + |
| 23 | + # Advances the system by one second: prints current status, decrements timers, |
| 24 | + # and updates states as needed, all in one giant procedural block. |
| 25 | + def run |
| 26 | + # Clear the screen each time (so this is consistent with the original request). |
| 27 | + # You could skip this if you don't want console clearing in tests, but it's here for completeness. |
| 28 | + system("clear") || system("cls") |
| 29 | + |
| 30 | + # Print traffic lights |
| 31 | + @lights.each do |light| |
| 32 | + # Quick inline way to colour states, done in a messy way rather than a dedicated method. |
| 33 | + state_colour = case light[:state] |
| 34 | + when "red" then "\e[31m#{light[:state]}\e[0m" |
| 35 | + when "green" then "\e[32m#{light[:state]}\e[0m" |
| 36 | + when "amber" then "\e[33m#{light[:state]}\e[0m" |
| 37 | + else light[:state] |
| 38 | + end |
| 39 | + |
| 40 | + puts "Direction: #{light[:direction]}, State: #{state_colour}, Time left: #{light[:timer]}s" |
| 41 | + end |
| 42 | + |
| 43 | + # Update timers and transition states in one big chunk |
| 44 | + @lights.each do |light| |
| 45 | + light[:timer] -= 1 |
| 46 | + if light[:timer] <= 0 |
| 47 | + # Big case statement for state transitions |
| 48 | + case light[:state] |
| 49 | + when "red" |
| 50 | + # Switch from red -> green |
| 51 | + light[:state] = "green" |
| 52 | + light[:timer] = 8 |
| 53 | + @pedestrian_signals[light[:direction]] = false |
| 54 | + # Force the opposite light red if it's green or amber |
| 55 | + opposite = @lights.find { |l| l[:direction] != light[:direction] } |
| 56 | + if %w[green amber].include?(opposite[:state]) |
| 57 | + opposite[:state] = "red" |
| 58 | + opposite[:timer] = 10 |
| 59 | + @pedestrian_signals[opposite[:direction]] = true |
| 60 | + end |
| 61 | + |
| 62 | + when "green" |
| 63 | + # Switch from green -> amber |
| 64 | + light[:state] = "amber" |
| 65 | + light[:timer] = 3 |
| 66 | + @pedestrian_signals[light[:direction]] = false |
| 67 | + |
| 68 | + when "amber" |
| 69 | + # Switch from amber -> red |
| 70 | + light[:state] = "red" |
| 71 | + light[:timer] = 10 |
| 72 | + @pedestrian_signals[light[:direction]] = true |
| 73 | + |
| 74 | + else |
| 75 | + # Fallback |
| 76 | + light[:state] = "red" |
| 77 | + light[:timer] = 10 |
| 78 | + @pedestrian_signals[light[:direction]] = true |
| 79 | + end |
| 80 | + end |
| 81 | + end |
| 82 | + |
| 83 | + # Print pedestrian signals |
| 84 | + puts "\nPedestrian signals:" |
| 85 | + @pedestrian_signals.each do |direction, can_walk| |
| 86 | + signal_text = can_walk ? "\e[32mWALK\e[0m" : "\e[31mDON'T WALK\e[0m" |
| 87 | + puts " #{direction}: #{signal_text}" |
| 88 | + end |
| 89 | + |
| 90 | + puts "---------------------------------" |
| 91 | + end |
| 92 | +end |
| 93 | + |
| 94 | +# Only run the perpetual loop if this file is executed directly. |
| 95 | +# Each call to #run advances the system by one second. Tests can call #run manually. |
| 96 | +if $PROGRAM_NAME == __FILE__ |
| 97 | + system = TrafficLightSystem.new |
| 98 | + loop do |
| 99 | + system.run |
| 100 | + sleep 1 |
| 101 | + end |
| 102 | +end |
0 commit comments