Skip to content

Commit

Permalink
16 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
gregawoods committed Dec 19, 2023
1 parent 8982d40 commit 40c9d01
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
37 changes: 26 additions & 11 deletions lib/days/16.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,31 @@ def pos

def part1(input)
grid = parse_grid(input)
visited = Set.new

beams = [
Beam.new(0, 0, :right)
]
solve(grid, Beam.new(0, 0, :right))
end

def part2(input)
grid = parse_grid(input)

values = []

grid.length.times do |x|
values << solve(grid, Beam.new(x, 0, :down))
values << solve(grid, Beam.new(x, grid.first.length - 1, :up))
end

grid.first.length.times do |y|
values << solve(grid, Beam.new(0, y, :right))
values << solve(grid, Beam.new(grid.length - 1, y, :right))
end

values.max
end

def solve(grid, initial)
visited = Set.new
beams = [initial]

loop do
new_beams = []
Expand All @@ -39,12 +59,11 @@ def part1(input)
if beam.x.negative? ||
beam.x >= grid.length ||
beam.y.negative? ||
beam.y >= grid.first.length
beam.y >= grid.first.length ||
visited.include?(beam)
next
end

next if visited.include?(beam)

visited.add(beam)

case grid[beam.x][beam.y]
Expand Down Expand Up @@ -97,8 +116,4 @@ def part1(input)
visited.map(&:pos).uniq.length
end

def part2(input)
'TODO'
end

end
2 changes: 1 addition & 1 deletion test/16_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ def test_part1
end

def test_part2
assert_equal @day.part2(@data), ''
assert_equal @day.part2(@data), 51
end
end

0 comments on commit 40c9d01

Please sign in to comment.