From 78e6cb25099586813a08730dbda258e82f03a28c Mon Sep 17 00:00:00 2001 From: Jeff Yates Date: Tue, 15 Dec 2020 10:01:13 -0500 Subject: [PATCH] finished --- day11/solution1.rb | 48 ++++++++++++++++++++++++++++++++----------- day11/solution2.rb | 51 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+), 12 deletions(-) create mode 100755 day11/solution2.rb diff --git a/day11/solution1.rb b/day11/solution1.rb index 1f39f0b..ad5acf8 100755 --- a/day11/solution1.rb +++ b/day11/solution1.rb @@ -1,27 +1,51 @@ #!/bin/env ruby require 'pry' +def convert (seats) + seats.map! do |row| + row.map! do |seat| + case seat + when 0 then 'L' + when 1 then '#' + when nil then '.' + end + end + row.reduce(:+) + end +end def fill_seats (seats) output = Array.new(seats.length, Array.new(seats[0].length, nil)) output.map!.with_index do |row, i| - row.each.with_index do |seat, j| - next if !seats[i][j] - adjecents = 0 - adjecents += seats[i+1][j-1..j+1].compact.sum if i < seats.length - 1 - adjecents += seats[i][j-1].to_i if j > 0 - adjecents += seats[i-1][j-1..j+1].compact.sum if i > 0 - adjecents += seats[i][j+1].to_i if j < seats[i].length - puts "#{i},#{j}:#{adjecents}" - adjecents >= 4 ? 0 : 1 + row.map.with_index do |seat, j| + next if seats[i][j] == nil + i == 0 ? row_min = 0 : row_min = i - 1 + i == seats.length - 1 ? row_max = -1 : row_max = i + 1 + j == 0 ? col_min = 0 : col_min = j - 1 + j == seats[0].length - 1 ? col_max = -1 : col_max = j + 1 + + row_range = (row_min..row_max) + col_range = (col_min..col_max) + adjecents = seats[row_range].map {|line| line[col_range]}.flatten.compact.sum - seats[i][j] + combo = {:occupied? => seats[i][j], :adjecents => adjecents} + case + when combo[:occupied?] == 0 && combo[:adjecents] == 0 then 1 + when combo[:occupied?] == 0 && combo[:adjecents] != 0 then 0 + when combo[:occupied?] == 1 && combo[:adjecents] < 4 then 1 + when combo[:occupied?] == 1 && combo[:adjecents] >= 4 then 0 + end end end return output end -seats = File.readlines('./input.sample').map(&:strip) +seats = File.readlines('./input').map(&:strip) .map! {|line| line.split('') } .map! {|line| line.map { |char| char == 'L' ? 0 : nil }} -#next = fill_seats(seats) -binding.pry \ No newline at end of file +steps = [] +steps[0] = seats +until steps[-1] == steps[-2] do + steps.push(fill_seats(steps[-1])) +end +p steps[-1].map(&:compact).flatten.sum diff --git a/day11/solution2.rb b/day11/solution2.rb new file mode 100755 index 0000000..ad5acf8 --- /dev/null +++ b/day11/solution2.rb @@ -0,0 +1,51 @@ +#!/bin/env ruby +require 'pry' +def convert (seats) + seats.map! do |row| + row.map! do |seat| + case seat + when 0 then 'L' + when 1 then '#' + when nil then '.' + end + end + row.reduce(:+) + end +end + +def fill_seats (seats) + output = Array.new(seats.length, Array.new(seats[0].length, nil)) + output.map!.with_index do |row, i| + row.map.with_index do |seat, j| + next if seats[i][j] == nil + i == 0 ? row_min = 0 : row_min = i - 1 + i == seats.length - 1 ? row_max = -1 : row_max = i + 1 + j == 0 ? col_min = 0 : col_min = j - 1 + j == seats[0].length - 1 ? col_max = -1 : col_max = j + 1 + + row_range = (row_min..row_max) + col_range = (col_min..col_max) + adjecents = seats[row_range].map {|line| line[col_range]}.flatten.compact.sum - seats[i][j] + combo = {:occupied? => seats[i][j], :adjecents => adjecents} + case + when combo[:occupied?] == 0 && combo[:adjecents] == 0 then 1 + when combo[:occupied?] == 0 && combo[:adjecents] != 0 then 0 + when combo[:occupied?] == 1 && combo[:adjecents] < 4 then 1 + when combo[:occupied?] == 1 && combo[:adjecents] >= 4 then 0 + end + end + end + return output +end + + +seats = File.readlines('./input').map(&:strip) + .map! {|line| line.split('') } + .map! {|line| line.map { |char| char == 'L' ? 0 : nil }} + +steps = [] +steps[0] = seats +until steps[-1] == steps[-2] do + steps.push(fill_seats(steps[-1])) +end +p steps[-1].map(&:compact).flatten.sum