From b62bf80b8678fbcd5076922b0b2693f90f7fdd03 Mon Sep 17 00:00:00 2001 From: Alpha Chen Date: Thu, 22 Dec 2022 21:39:59 -0800 Subject: [PATCH] [2022][ruby][23.x] --- 2022/ruby/day_23.rb | 50 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 2022/ruby/day_23.rb diff --git a/2022/ruby/day_23.rb b/2022/ruby/day_23.rb new file mode 100644 index 0000000..6278326 --- /dev/null +++ b/2022/ruby/day_23.rb @@ -0,0 +1,50 @@ +grove = {} +ARGF.read.lines(chomp: true).each.with_index do |row, y| + row.chars.each.with_index do |c, x| + grove[[y,x]] = c == ?# + end +end + +dirs = [ + [[-1, -1], [-1, 0], [-1, 1]], + [[ 1, -1], [ 1, 0], [ 1, 1]], + [[-1, -1], [ 0, -1], [ 1, -1]], + [[-1, 1], [ 0, 1], [ 1, 1]], +] + +(1..).each do |i| + proposals = grove.select { _2 } + .to_h {|(y,x),_| + to = if dirs.flatten(1).none? {|dy,dx| grove[[y+dy, x+dx]] } + [y, x] + elsif dir = dirs.find {|adj| adj.none? {|dy,dx| grove[[y+dy, x+dx]] }} + dy, dx = dir[1] + [y+dy, x+dx] + else + [y, x] + end + [[y,x], to] + } + + proposals.each.with_object(Hash.new {|h,k| h[k] = []}) {|(from, to), conflicts| + conflicts[to] << from + }.select { _2.size > 1 }.values.flatten(1).each do |elf| + proposals[elf] = elf + end + + if proposals.all? { _1 == _2 } + puts i + exit + end + + proposals.each do |from, to| + grove[from] = false + grove[to] = true + end + + dirs.push(dirs.shift) +end + +# y = grove.select { _2 }.keys.map(&:first).minmax +# x = grove.select { _2 }.keys.map(&:last).minmax +# p Range.new(*y).sum {|y| Range.new(*x).count {|x| !grove[[y,x]] }}