|
|
@ -1,19 +1,41 @@
|
|
|
|
|
|
|
|
# all rotations are clockwise
|
|
|
|
|
|
|
|
# all flips are vertical
|
|
|
|
|
|
|
|
|
|
|
|
class Tile
|
|
|
|
class Tile
|
|
|
|
SIZE = 10
|
|
|
|
attr_reader :id, :raw
|
|
|
|
|
|
|
|
|
|
|
|
attr_reader :raw
|
|
|
|
def initialize(id, raw)
|
|
|
|
|
|
|
|
@id, @raw = id, raw
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def initialize(raw)
|
|
|
|
def [](y, x)
|
|
|
|
@raw = raw
|
|
|
|
@raw.fetch(y).fetch(x)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def edges
|
|
|
|
def edge_keys
|
|
|
|
{
|
|
|
|
{
|
|
|
|
top: @raw.fetch(0),
|
|
|
|
top: @raw.first,
|
|
|
|
bottom: @raw.fetch(SIZE-1),
|
|
|
|
down: @raw.last,
|
|
|
|
left: @raw.transpose.fetch(0),
|
|
|
|
left: @raw.transpose.first,
|
|
|
|
right: @raw.transpose.fetch(SIZE-1),
|
|
|
|
right: @raw.transpose.last,
|
|
|
|
}
|
|
|
|
}.transform_values(&:join)
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def permutations
|
|
|
|
|
|
|
|
(0..3).map {|i|
|
|
|
|
|
|
|
|
i.times.inject(self) {|x,_| x.rotated }
|
|
|
|
|
|
|
|
}.flat_map {|x| [x, x.flipped] }
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def rotated
|
|
|
|
|
|
|
|
self.class.new(
|
|
|
|
|
|
|
|
@id,
|
|
|
|
|
|
|
|
@raw.map {|row| [row].transpose }.reverse.inject(&:zip).map(&:flatten),
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def flipped
|
|
|
|
|
|
|
|
self.class.new(@id, @raw.reverse)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
def to_s
|
|
|
@ -21,35 +43,74 @@ class Tile
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
input = ARGF.read
|
|
|
|
Edge = Struct.new(*%i[ key from to rotations is_mirrored ]) do
|
|
|
|
|
|
|
|
def mirrored?
|
|
|
|
|
|
|
|
is_mirrored
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tiles = ARGF.read
|
|
|
|
.scan(/Tile (\d+):\n((?:[.#]+\n)+)/m)
|
|
|
|
.scan(/Tile (\d+):\n((?:[.#]+\n)+)/m)
|
|
|
|
.to_h
|
|
|
|
.to_h
|
|
|
|
.transform_keys(&:to_i)
|
|
|
|
|
|
|
|
.transform_values {|v| v.split("\n").map {|row| row.chars } }
|
|
|
|
.transform_values {|v| v.split("\n").map {|row| row.chars } }
|
|
|
|
.transform_values {|v| Tile.new(v) }
|
|
|
|
.map {|k,v| [k, Tile.new(k, v)] }
|
|
|
|
|
|
|
|
.to_h
|
|
|
|
edges = input.flat_map {|id,tile|
|
|
|
|
|
|
|
|
tile.edges.map {|edge,data| ["#{id}_#{edge}", data.join] }
|
|
|
|
keys_to_tiles = Hash.new {|h,k| h[k] = [] }
|
|
|
|
}.to_h
|
|
|
|
tiles.values.each do |tile|
|
|
|
|
|
|
|
|
tile.edge_keys.values.each do |key|
|
|
|
|
inverted_edges = edges.each.with_object(Hash.new {|h,k| h[k] = [] }) do |(id,data),hash|
|
|
|
|
keys_to_tiles[key] << tile.id
|
|
|
|
hash[data] << id
|
|
|
|
keys_to_tiles[key.reverse] << tile.id
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
matches = edges.map {|id,data|
|
|
|
|
|
|
|
|
[
|
|
|
|
start_corner = tiles.values.find {|tile|
|
|
|
|
id,
|
|
|
|
tile.edge_keys.values_at(:left, :top).all? {|key| keys_to_tiles.fetch(key).size == 1 }
|
|
|
|
[
|
|
|
|
}
|
|
|
|
inverted_edges[data],
|
|
|
|
|
|
|
|
inverted_edges[data.reverse],
|
|
|
|
image = [[start_corner]]
|
|
|
|
].flatten.compact.reject {|other| other == id },
|
|
|
|
until image.map(&:size).sum == tiles.size
|
|
|
|
]
|
|
|
|
current = image.last.last
|
|
|
|
}.to_h
|
|
|
|
right_key = current.edge_keys.fetch(:right)
|
|
|
|
|
|
|
|
if right_id = keys_to_tiles.fetch(right_key, nil).find {|id| id != current.id }
|
|
|
|
corners = input.keys.select {|id|
|
|
|
|
right_tile = tiles.fetch(right_id).permutations.find {|p| p.edge_keys.fetch(:left) == right_key }
|
|
|
|
%w[ top bottom left right ].count {|edge|
|
|
|
|
image.last << right_tile
|
|
|
|
matches.fetch("#{id}_#{edge}").empty?
|
|
|
|
else
|
|
|
|
} == 2
|
|
|
|
current = image.last.first
|
|
|
|
|
|
|
|
down_key = current.edge_keys.fetch(:down)
|
|
|
|
|
|
|
|
break unless down_id = keys_to_tiles.fetch(down_key).find {|id| id != current.id }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
down_tile = tiles.fetch(down_id).permutations.find {|p| p.edge_keys.fetch(:top) == down_key }
|
|
|
|
|
|
|
|
image << [down_tile]
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
full_raw = image.map {|row| row.map {|tile|
|
|
|
|
|
|
|
|
tile.raw[1..-2].map {|row| row[1..-2] }
|
|
|
|
|
|
|
|
}}.flat_map {|row| row.inject(&:zip).map(&:flatten) }
|
|
|
|
|
|
|
|
full_tile = Tile.new(nil, full_raw)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sea_monster = <<~SEA_MONSTER.split("\n").map(&:chars)
|
|
|
|
|
|
|
|
#
|
|
|
|
|
|
|
|
# ## ## ###
|
|
|
|
|
|
|
|
# # # # # #
|
|
|
|
|
|
|
|
SEA_MONSTER
|
|
|
|
|
|
|
|
needle = sea_monster.flat_map.with_index {|row,y|
|
|
|
|
|
|
|
|
row.filter_map.with_index {|c,x|
|
|
|
|
|
|
|
|
c == ?# ? [y,x] : nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
num_monsters = full_tile.permutations.map {|tile|
|
|
|
|
|
|
|
|
needle_maxes = needle.transpose.map(&:max)
|
|
|
|
|
|
|
|
haystack = (0...tile.raw.size-needle_maxes.first).flat_map {|y|
|
|
|
|
|
|
|
|
(0...tile.raw.first.size-needle_maxes.last).map {|x|
|
|
|
|
|
|
|
|
[y, x]
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
haystack.count {|y,x|
|
|
|
|
|
|
|
|
needle.all? {|dy,dx| tile[y+dy, x+dx] == ?# }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}.sum
|
|
|
|
|
|
|
|
|
|
|
|
p corners.inject(&:*)
|
|
|
|
puts full_tile.raw.map {|row| row.count(?#) }.sum - (num_monsters * needle.size)
|
|
|
|