advent_of_code/2024_8.rb
Code:
# Advent of Code 2024, Day 8: Resonant Collinearity
# https://adventofcode.com/2024/day/8
require_relative './helpers.rb'
def d(a, b)
Point.new(a.x - b.x, a.y - b.y)
end
def part_one(grid)
g = copy_grid(grid)
(grid.flatten.uniq - ['.']).each do |f|
find_grid(grid, f).combination(2).each do |a, b|
set_grid(g, d(a, d(b, a)), '#')
set_grid(g, d(b, d(a, b)), '#')
end
end
g.flatten.count('#')
end
def part_two(grid)
g = copy_grid(grid)
(grid.flatten.uniq - ['.']).each do |f|
find_grid(grid, f).combination(2).each do |a, b|
d_a, d_b = d(b, a), d(a, b)
i, j = d(a, d_a), d(b, d_b)
i = d(i, d_a) while set_grid(g, i, '#')
j = d(j, d_b) while set_grid(g, j, '#')
end
end
g.flatten.count { |c| c != '.' }
end
def solve(data)
data.map!(&:chomp).map! { |l| l.chars }
# draw_grid(data) if data.size < 20
[part_one(copy_grid(data)), part_two(copy_grid(data))]
end
puts solve(get_dataset(year: 2024, day: 8, type: 'example'))
# puts solve(get_dataset(year: 2024, day: 8, type: 'example_2'))
puts solve(get_dataset(year: 2024, day: 8, type: 'example_3'))
puts solve(get_dataset(year: 2024, day: 8, type: 'input'))
Output:
14
34
3
9
278
1067