You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
image = ARGF.readlines(chomp: true).map(&:chars)
|
|
|
|
empty_rows = image.filter_map.with_index {|row,y| row.all? { _1 == ?. } ? y : nil }
|
|
|
|
empty_cols = image.transpose.filter_map.with_index {|col,x| col.all? { _1 == ?. } ? x : nil }
|
|
|
|
|
|
|
|
galaxies = []
|
|
|
|
y = x = 0
|
|
|
|
image.each.with_index do |row, yy|
|
|
|
|
x = 0
|
|
|
|
row.each.with_index do |elem, xx|
|
|
|
|
galaxies << [y,x] if elem == ?#
|
|
|
|
x += 1
|
|
|
|
x += 999_999 if empty_cols.include?(xx)
|
|
|
|
end
|
|
|
|
|
|
|
|
y += 1
|
|
|
|
y += 999_999 if empty_rows.include?(yy)
|
|
|
|
end
|
|
|
|
|
|
|
|
p galaxies.combination(2).sum {|a, b|
|
|
|
|
a.zip(b).sum { (_1 - _2).abs }
|
|
|
|
}
|
|
|
|
|
|
|
|
# original part one
|
|
|
|
|
|
|
|
# image = ARGF.read
|
|
|
|
# image.gsub!(/^(\.+)$/m, "\\1\n\\1")
|
|
|
|
# image = image.split("\n").map(&:chars).transpose.map(&:join).join("\n")
|
|
|
|
# image.gsub!(/^(\.+)$/m, "\\1\n\\1")
|
|
|
|
# image = image.split("\n").map(&:chars)
|
|
|
|
|
|
|
|
# galaxies = image.flat_map.with_index {|row, y|
|
|
|
|
# row.filter_map.with_index {|elem, x| elem == ?# ? [y,x] : nil }
|
|
|
|
# }
|
|
|
|
|
|
|
|
# p galaxies.combination(2).sum {|a, b|
|
|
|
|
# a.zip(b).sum { (_1 - _2).abs }
|
|
|
|
# }
|