From bb2ee520ab184deeb6d05c3c5639fb26d0b41971 Mon Sep 17 00:00:00 2001 From: Wesley Moore Date: Mon, 11 Dec 2023 19:19:40 +1000 Subject: [PATCH] 2023: Day 11, part 2 --- 2023/day11.rb | 38 ++++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/2023/day11.rb b/2023/day11.rb index 521e3ed..92d4324 100644 --- a/2023/day11.rb +++ b/2023/day11.rb @@ -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}"