Skip to content

Instantly share code, notes, and snippets.

@perumal
Last active May 5, 2019 13:41
Show Gist options
  • Save perumal/1a0b579ae0c28dbd198296aff6f02dfd to your computer and use it in GitHub Desktop.
Save perumal/1a0b579ae0c28dbd198296aff6f02dfd to your computer and use it in GitHub Desktop.
Seat Allot Project Code
num_of_passengers = gets
if num_of_passengers != nil
num_of_passengers = num_of_passengers.chomp.to_i
else
exit(0)
end
seat_structure_raw_input = gets(nil)
if seat_structure_raw_input != nil
seat_structure_raw_input = seat_structure_raw_input.chomp
else
exit(0)
end
orig_seat_structure = seat_structure_raw_input.split("\n").map{|arr| arr.split(" ").map{ |rc| rc.to_i } }
####################################################################################################
class SeatLayout
@number_of_seat_rows = 0
@number_of_seat_columns = 0
@maximum_bookable_seats = 0
@passed_seat_structure_data = []
@current_num_passengers = 0
@raw_seat_layout = []
@booked_seat_layout = []
#can be changed
@seating_priority = ["-A-", "-W-", "-C-"]
module SeatLayoutType
RAW = 1
BOOKED = 2
end
def initialize
@seating_priority = ["-A-", "-W-", "-C-"]
end
def set_seating_priority(seating_priority_param)
@seating_priority = seating_priority_param
end
def set_seat_layout_data(seat_layout_params)
@passed_seat_structure_data = clean_seat_layout_data(seat_layout_params)
create_raw_seat_layout()
end
def clean_seat_layout_data(seat_layout_params)
@maximum_bookable_seats = 0
seat_layout_data = []
seat_layout_params.each do |seat_row|
temp_seat_row = []
seat_row.each do |seat_column|
if (seat_column > 0)
temp_seat_row << seat_column
else
temp_seat_row = []
break
end
end
if (temp_seat_row.length > 0)
seat_layout_data << temp_seat_row
end
end
seat_layout_data
end
def create_raw_seat_layout
max_seat_column = @passed_seat_structure_data.length
max_row = @passed_seat_structure_data.map{ |rc| rc[1]}.max
return if max_row == nil
columns_arr = @passed_seat_structure_data.map{|rc| rc[1]}
rows_arr = @passed_seat_structure_data.map{|rc| rc[0]}
seating_arr = []
(1..max_row).each_with_index do |mrow, mindex|
seating_row = []
rows_arr.each_with_index do |trow, index|
(1..trow).each do |row|
if (mrow > columns_arr[index])
seating_row << "---"
else
if (row == 1) && (index == 0)
if (trow == 1)
seating_row << "-A-"
else
seating_row << "-W-"
end
elsif (row == trow) && (index == (rows_arr.length - 1))
if (trow == 1)
seating_row << "-A-"
else
seating_row << "-W-"
end
elsif (row == trow) || (row == 1)
seating_row << "-A-"
else
seating_row << "-C-"
end
@maximum_bookable_seats = @maximum_bookable_seats + 1
end
end
if (index != (rows_arr.length - 1))
seating_row << "|"
end
end
seating_arr << seating_row
end
@raw_seat_layout = seating_arr
end
def book_seats(num_of_passengers_param)
@current_num_passengers = num_of_passengers_param
if (@current_num_passengers > @maximum_bookable_seats)
@current_num_passengers = @maximum_bookable_seats
end
seated_layout = @raw_seat_layout
current_seated_passengers = 1
seating_priority_arr = @seating_priority
while (current_seated_passengers < @current_num_passengers)
curent_seating_priority = seating_priority_arr.shift()
break unless (0..seated_layout.length).each do |srow|
seated_col = []
break unless (0..@raw_seat_layout[srow].length).each do |scol|
seat_type = @raw_seat_layout[srow][scol]
if seat_type == curent_seating_priority
seated_layout[srow][scol] = current_seated_passengers.to_s.rjust(3, "0")
current_seated_passengers = current_seated_passengers + 1
break if (current_seated_passengers > @current_num_passengers)
end
end
seated_layout << seated_col
end
end
@booked_seat_layout = seated_layout
end
def print_raw_seat_layout
print_seat_layout(SeatLayoutType::RAW)
end
def print_booked_seat_layout
print_seat_layout(SeatLayoutType::BOOKED)
end
private
def print_seat_layout(layout_type)
use_seated_layout = (layout_type == SeatLayoutType::RAW) ? @raw_seat_layout : @booked_seat_layout
use_seated_layout.each do |seat_row_data|
seat_row_data.each do |seat|
print seat + " "
end
puts " "
end
STDOUT.flush
end
end
####################################################################################################
seatLayout = SeatLayout.new
seatLayout.set_seat_layout_data(orig_seat_structure)
seatLayout.book_seats(num_of_passengers)
seatLayout.print_booked_seat_layout()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment