Created
December 11, 2023 16:48
-
-
Save carlwiedemann/9f786c353823dce12efcfe29eb5e2185 to your computer and use it in GitHub Desktop.
Advent of Code 2023 day011.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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