20_jurassic_jigsaw.rb
# Could just try all eight, but let's be a bit more nuanced.
Could just try all eight, but let's be a bit more nuanced.
Could just try all eight but let ' s be a bit more nuanced .
def make_match_border!(my_border, target)
    already_matching = @borders.select { |_, v|
      v == target || v.reverse == target
    }
    raise "making #@id #@borders match #{target} has candidates #{already_matching}" unless already_matching.size == 1
    current = already_matching.keys[0]
    new_img = @img
    if current != my_border && current != OPPOSITE[my_border]
      new_img = transpose_str(new_img)
      current = TRANSPOSE[current]
    end
    if current != my_border
      new_img = current == :top || current == :bottom ? new_img.reverse : fliph(new_img)
      current = OPPOSITE[current]
    end
    if border(new_img, my_border) != target
      new_img = current == :top || current == :bottom ? fliph(new_img) : new_img.reverse
    end
    if (new_border = border(new_img, my_border)) != target
      raise "making #@id #@borders match #{target} resulted in #{new_border} - originally #{already_matching}, now #{current}"
    end
    if @img != new_img
      @borders = mkborders(new_img).freeze
      @img = new_img.map(&:freeze).freeze
    end
  end
def make_match_border! ( my_border , target ) already_matching = @borders . select { | _ , v | v == target || v . reverse == target } raise "making #@id #@borders match #{target} has candidates #{already_matching}" unless already_matching . size == 1 current = already_matching . keys [ 0 ] new_img = @img if current != my_border && current != OPPOSITE [ my_border ] new_img = transpose_str ( new_img ) current = TRANSPOSE [ current ] end if current != my_border new_img = current == :top || current == :bottom ? new_img . reverse : fliph ( new_img ) current = OPPOSITE [ current ] end if border ( new_img , my_border ) != target new_img = current == :top || current == :bottom ? fliph ( new_img ) : new_img . reverse end if ( new_border = border ( new_img , my_border ) ) != target raise "making #@id #@borders match #{target} resulted in #{new_border} - originally #{already_matching}, now #{current}" end if @img != new_img @borders = mkborders ( new_img ) . freeze @img = new_img . map ( & :freeze ) . freeze end end
Could just try all eight, but let's be a bit more nuanced.
Could just try all eight but let ' s be a bit more nuanced .
# Check if we need to rotate 90/270 degrees # Now we're either on the same border or opposite # Now we're on the same border but might be reversed.
my_border
<not_specific>
target
<not_specific>