Skip to content

Instantly share code, notes, and snippets.

@carlwiedemann
Created December 11, 2023 16:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlwiedemann/9f786c353823dce12efcfe29eb5e2185 to your computer and use it in GitHub Desktop.
Save carlwiedemann/9f786c353823dce12efcfe29eb5e2185 to your computer and use it in GitHub Desktop.
Advent of Code 2023 day011.rb
require_relative "main"
module Day011
INPUT = File.read('INPUT.txt')
grid = INPUT.to_grid
##########
# Part 1 #
##########
empty_rows = (0...grid.y_length).filter_map do |y|
(grid.row(y).all? { _1 == "." }) ? y : nil
end
empty_cols = (0...grid.x_length).filter_map do |x|
(grid.column(x).all? { _1 == "." }) ? x : nil
end
# @type [Array[V]]
galaxies = []
grid.each do |v, value|
galaxies.push(v) if value == "#"
end
t1 = 0
t2 = 0
i = 0
while i < galaxies.length
j = i + 1
while j < galaxies.length
a = galaxies[i]
b = galaxies[j]
x_expand = ([a.x, b.x].min..[a.x, b.x].max).to_a.filter { empty_cols.include?(_1) }.count
y_expand = ([a.y, b.y].min..[a.y, b.y].max).to_a.filter { empty_rows.include?(_1) }.count
manhattan = (b - a).manhattan
t1 += x_expand + y_expand + manhattan
t2 += x_expand * (1000000 - 1) + y_expand * (1000000 - 1) + manhattan
j += 1
end
i += 1
end
answer1 = t1
pp answer1
##########
# Part 2 #
##########
answer2 = t2
pp answer2
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment