Skip to content

Commit

Permalink
Day 8 - Puzzle 2
Browse files Browse the repository at this point in the history
  • Loading branch information
ariejan committed Dec 8, 2024
1 parent 2997c0b commit 7cd65ad
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
35 changes: 32 additions & 3 deletions lib/solutions/day_08.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,14 @@ def part_one(input)
@antinodes.flatten.count(true)
end

def part_two(_input)
0
def part_two(input)
parse_input(input)

@nodes.each_pair do |_node, coords|
find_antinodes2(coords)
end

@antinodes.flatten.count(true)
end

def mark_antinode(x, y)
Expand All @@ -25,7 +31,7 @@ def mark_antinode(x, y)

if x < 0 || x >= @w || y < 0 || y >= @h
# puts '- OUT'
return
return false
end

# puts ' - OK'
Expand All @@ -43,6 +49,29 @@ def find_antinodes(coords)
end
end

def find_antinodes2(coords)
# Loop through all coordinate pemutations
coords.permutation(2).to_a.each do |a, b|
# Mark this node as antinode
mark_antinode(a[0], a[1])

dx = b[0] - a[0]
dy = b[1] - a[1]

ox = a[0]
oy = a[1]

multiplier = 1

loop do
in_grid = mark_antinode(ox - (dx * multiplier), oy - (dy * multiplier))
break unless in_grid

multiplier += 1
end
end
end

def parse_input(input)
# Parse grid
@grid = input.split("\n").map(&:chars)
Expand Down
2 changes: 1 addition & 1 deletion spec/solutions/day_08_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

describe '#part_two' do
it 'calculates the correct solutions for part two' do
expect(subject.part_two(input)).to eq(0)
expect(subject.part_two(input)).to eq(34)
end
end
end

0 comments on commit 7cd65ad

Please sign in to comment.