mirror of
https://github.com/wezm/advent-of-code.git
synced 2024-11-14 03:28:01 +00:00
2023: Day 11, part 2
This commit is contained in:
parent
083b47c23b
commit
bb2ee520ab
1 changed files with 34 additions and 4 deletions
|
@ -1,9 +1,10 @@
|
|||
MULTIPLIER = ENV["MULTIPLIER"].to_i || 2
|
||||
input = ARGF.read.strip
|
||||
by_row = input.lines.map(&:rstrip).map { |line| line.split("") }
|
||||
|
||||
def needs_expand(data)
|
||||
expand = []
|
||||
data.each_with_index { |row, i| expand << i if row.all? { |c| c == "." } }
|
||||
data.each_with_index { |row, i| expand << i if row.all? { |c| c == "." || c == "," } }
|
||||
expand
|
||||
end
|
||||
|
||||
|
@ -16,16 +17,17 @@ end
|
|||
# do expansion
|
||||
row_expand = needs_expand(by_row)
|
||||
row_expand.reverse.each do |i|
|
||||
by_row.insert(i, by_row[i].dup)
|
||||
by_row[i] = ("," * by_row[i].length).split("") # add expansion marker
|
||||
end
|
||||
|
||||
by_column = by_row.transpose
|
||||
col_expand = needs_expand(by_column)
|
||||
col_expand.reverse.each do |i|
|
||||
by_column.insert(i, by_column[i].dup)
|
||||
by_column[i] = ("," * by_column[i].length).split("") # add expansion marker
|
||||
end
|
||||
|
||||
cosmos = by_column.transpose
|
||||
print_cosmos(cosmos)
|
||||
|
||||
# find galaxies
|
||||
galaxies = []
|
||||
|
@ -39,4 +41,32 @@ def galaxy_distance(a, b)
|
|||
(a[0] - b[0]).abs + (a[1] - b[1]).abs
|
||||
end
|
||||
|
||||
puts "Part 1: #{galaxies.combination(2).map { |a, b| galaxy_distance(a, b) }.sum}"
|
||||
def cosmos_at(x, y, cosmos)
|
||||
cosmos[y][x]
|
||||
end
|
||||
|
||||
distances = galaxies.combination(2).map do |combo|
|
||||
a, b = combo.sort
|
||||
distance = galaxy_distance(a, b)
|
||||
|
||||
# see how many millions are crossed in x
|
||||
x_millions = (a[0]..b[0]).filter_map do |x|
|
||||
loc = cosmos_at(x, a[1], cosmos)
|
||||
loc if loc == ","
|
||||
end
|
||||
|
||||
# now y component
|
||||
range = if a[1] > b[1]
|
||||
b[1]..a[1]
|
||||
else
|
||||
a[1]..b[1]
|
||||
end
|
||||
y_millions = range.filter_map do |y|
|
||||
loc = cosmos_at(b[0], y, cosmos)
|
||||
loc if loc == ","
|
||||
end
|
||||
|
||||
distance + ((x_millions.length + y_millions.length) * MULTIPLIER) - x_millions.length - y_millions.length
|
||||
end
|
||||
|
||||
puts "Sum of distances with multiplier #{MULTIPLIER}: #{distances.sum}"
|
||||
|
|
Loading…
Reference in a new issue