Skip to content

Instantly share code, notes, and snippets.

@carlwiedemann
Last active December 8, 2024 06:36
Show Gist options
  • Save carlwiedemann/ddcf4823c281dd42892aa5a93fe6d9b6 to your computer and use it in GitHub Desktop.
Save carlwiedemann/ddcf4823c281dd42892aa5a93fe6d9b6 to your computer and use it in GitHub Desktop.
Advent of Code 2024 day008.rb
require_relative "main"
module Day008
INPUT = File.read("INPUT.txt")
EMPTY = "."
grid = INPUT.to_grid
h = grid.each_with_object(Hash.new { |h, k| h[k] = [] }) { |v, c, memo| memo[c].push(v) if c != EMPTY }
##########
# Part 1 #
##########
ns = h.values.each_with_object([]) do |vs, memo|
i = 0
while i < vs.length
j = i + 1
while j < vs.length
d = vs[j] - vs[i]
a = vs[j] + d
b = vs[i] - d
memo.push(a) if grid.in_bounds?(a) && !memo.include?(a)
memo.push(b) if grid.in_bounds?(b) && !memo.include?(b)
j += 1
end
i += 1
end
end
answer1 = ns.count
pp answer1
##########
# Part 2 #
##########
ns = h.values.each_with_object([]) do |vs, memo|
i = 0
while i < vs.length
j = i + 1
while j < vs.length
d = vs[j] - vs[i]
a = vs[j]
b = vs[i]
loop do
memo.push(a) unless memo.include?(a)
a += d
break unless grid.in_bounds?(a)
end
loop do
memo.push(b) unless memo.include?(b)
b -= d
break unless grid.in_bounds?(b)
end
j += 1
end
i += 1
end
end
answer2 = ns.count
pp answer2
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment