Skip to content

Commit

Permalink
Add 2024, day 8
Browse files Browse the repository at this point in the history
  • Loading branch information
bewuethr committed Dec 8, 2024
1 parent 23fec8d commit 0166946
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
34 changes: 34 additions & 0 deletions 2024/day08/day08a
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env ruby

Vec2 = Struct.new(:x, :y) do
def +(other) = Vec2.new(x + other.x, y + other.y)

def -(other) = Vec2.new(x - other.x, y - other.y)
end

lines = File.readlines(ARGV[0]).map(&:chomp)

y_range = (0...lines.length)
x_range = (0...lines[0].length)

antennas = Hash.new { |h, k| h[k] = [] }

lines.each_with_index do |row, y|
row.chars.each_with_index do |loc, x|
next if loc == "."
antennas[loc] << Vec2.new(x, y)
end
end

antinodes = Set.new

antennas.each_pair do |freq, coords|
coords.combination(2) do |v1, v2|
candidates = [v1 - (v2 - v1), v2 - (v1 - v2)]
candidates.each do |v|
antinodes.add(v) if y_range.include?(v.y) && x_range.include?(v.x)
end
end
end

puts antinodes.size
41 changes: 41 additions & 0 deletions 2024/day08/day08b
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env ruby

Vec2 = Struct.new(:x, :y) do
def +(other) = Vec2.new(x + other.x, y + other.y)

def -(other) = Vec2.new(x - other.x, y - other.y)

def *(k) = Vec2.new(x * k, y * k)
end

lines = File.readlines(ARGV[0]).map(&:chomp)

y_range = (0...lines.length)
x_range = (0...lines[0].length)

antennas = Hash.new { |h, k| h[k] = [] }

lines.each_with_index do |row, y|
row.chars.each_with_index do |loc, x|
next if loc == "."
antennas[loc] << Vec2.new(x, y)
end
end

antinodes = Set.new

antennas.each_pair do |freq, coords|
coords.combination(2) do |v1, v2|
diffs = [v2 - v1, v1 - v2]
diffs.each do |v|
k = 0
candidate = v1 + v * k
while y_range.cover?(candidate.y) && x_range.cover?(candidate.x)
antinodes.add(candidate)
candidate += v
end
end
end
end

puts antinodes.size

0 comments on commit 0166946

Please sign in to comment.