diff --git a/2024/day08.rb b/2024/day08.rb new file mode 100644 index 0000000..a79efd9 --- /dev/null +++ b/2024/day08.rb @@ -0,0 +1,48 @@ +lines = STDIN.read.split("\n") +grid = lines.map { |line| line.chars } + +antennas = {} + +0.upto(grid.size - 1).each do |i| + 0.upto(grid.first.size - 1).each do |j| + (antennas[grid[i][j]] ||= []) << [i, j] if grid[i][j] != '.' + end +end + +# Part 1 +unique_locations = Set.new +antennas.each do |k, points| + points.combination(2).each do |((ai, aj), (bi, bj))| + antinodes = [ + [(ai - bi) + ai, (aj - bj) + aj], + [(bi - ai) + bi, (bj - aj) + bj] + ].filter { |(i, j)| i >= 0 && j >= 0 && i <= (grid.size - 1) && j <= (grid.first.size - 1) } + + antinodes.each { |a| unique_locations.add(a) } + end +end +puts unique_locations.size + +# Part 2 +antinodes = Set.new +antennas.each do |k, points| + points.combination(2).each do |((ai, aj), (bi, bj))| + n = 0 + while + i = n * (ai - bi) + ai + j = n * (aj - bj) + aj + break if i < 0 || j < 0 || i > (grid.size - 1) || j > (grid.first.size - 1) + antinodes.add([i, j]) + n += 1 + end + n = 0 + while + i = n * (bi - ai) + bi + j = n * (bj - aj) + bj + break if i < 0 || j < 0 || i > (grid.size - 1) || j > (grid.first.size - 1) + antinodes.add([i, j]) + n += 1 + end + end +end +puts antinodes.size diff --git a/2024/examples/day08.txt b/2024/examples/day08.txt new file mode 100644 index 0000000..78a1e91 --- /dev/null +++ b/2024/examples/day08.txt @@ -0,0 +1,12 @@ +............ +........0... +.....0...... +.......0.... +....0....... +......A..... +............ +............ +........A... +.........A.. +............ +............