parent
1118280a6e
commit
28e35196f5
@ -1,13 +1,37 @@
|
||||
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 }
|
||||
}
|
||||
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 }
|
||||
# }
|
||||
|
Loading…
Reference in new issue