Skip to content

Instantly share code, notes, and snippets.

@MikeRogers0
Created December 13, 2020 10:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MikeRogers0/ef82b97f3845c741608f29efe7819991 to your computer and use it in GitHub Desktop.
Save MikeRogers0/ef82b97f3845c741608f29efe7819991 to your computer and use it in GitHub Desktop.
aoc 2020 day 11
@rows = {}
File.open('input.real.txt').read.split("\n").compact.each_with_index do |row, index|
@rows[index] = {}
row.each_char.with_index do |element, element_index|
@rows[index][element_index] = element
end
end
@last_row = @rows.keys.last
@last_column = @rows[0].keys.last
def get_seats_around_ignoring_floor(row_index, seat_index, direction_row: 1, direction_seat: 1)
row = row_index
seat = seat_index
loop do
row += direction_row
seat += direction_seat
next if @comparision_rows.dig(row, seat) == '.'
return @comparision_rows.dig(row, seat)
end
end
def get_seats_around(row_index, seat_index)
[
get_seats_around_ignoring_floor(row_index, seat_index, direction_row: -1, direction_seat: -1),
get_seats_around_ignoring_floor(row_index, seat_index, direction_row: -1, direction_seat: 0),
get_seats_around_ignoring_floor(row_index, seat_index, direction_row: -1, direction_seat: 1),
get_seats_around_ignoring_floor(row_index, seat_index, direction_row: 0, direction_seat: -1),
get_seats_around_ignoring_floor(row_index, seat_index, direction_row: 0, direction_seat: 0),
get_seats_around_ignoring_floor(row_index, seat_index, direction_row: 0, direction_seat: 1),
get_seats_around_ignoring_floor(row_index, seat_index, direction_row: 1, direction_seat: -1),
get_seats_around_ignoring_floor(row_index, seat_index, direction_row: 1, direction_seat: 0),
get_seats_around_ignoring_floor(row_index, seat_index, direction_row: 1, direction_seat: 1),
].compact
end
def seat_people!
@rows.each do |row_index, row|
row.each do |seat_index, seat|
if seat == 'L'
if get_seats_around(row_index, seat_index).all? { |other_seat| other_seat == 'L' || other_seat == '.' }
@rows[row_index][seat_index] = '#'
end
elsif seat == '#'
if get_seats_around(row_index, seat_index).select { |other_seat| other_seat == '#' }.size > 5
@rows[row_index][seat_index] = 'L'
end
end
end
end
end
@replaced_rows = ''
@originial_rows = ''
index = 0
@comparision_rows = @rows.collect do |key, value|
[key, value.collect { |k,v| [k,v] }.to_h]
end.to_h
@originial_rows = @rows.values.collect { |rows| rows.values.join }.join("\n")
seat_people!
puts @rows.values.collect { |rows| rows.values.join }.join("\n")
while(@originial_rows != @replaced_rows) do
@originial_rows = @rows.values.collect { |rows| rows.values.join }.join("\n")
@comparision_rows = @rows.collect do |key, value|
[key, value.collect { |k,v| [k,v] }.to_h]
end.to_h
seat_people!
index += 1
@replaced_rows = @rows.values.collect { |rows| rows.values.join }.join("\n")
puts ""
puts @rows.values.collect { |rows| rows.values.join }.join("\n")
end
puts @rows.values.collect { |rows| rows.values.join }.join.each_char.to_a.tally
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment