Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- input = File.read("input.txt").split("\n")
- @width = input.size
- @height = input[0].size
- @grid = []
- @start = nil
- @dirs = [0-1i, 1+0i, 0+1i, -1+0i]
- @width.times do |y|
- @height.times do |x|
- case input[y][x]
- when ?#
- @grid << Complex(x,y)
- when ?^
- @start = Complex(x,y)
- end
- end
- end
- def inbounds?(pos)
- pos.real.between?(0, @width-1) && pos.imag.between?(0, @height-1)
- end
- def findPath
- pos = @start
- dir = 0
- visited = Set[pos]
- while inbounds?(pos) do
- until @grid.include?(pos + @dirs[dir]) do
- pos += @dirs[dir]
- break unless inbounds?(pos)
- visited << pos
- end
- dir = (dir+1) % 4
- end
- visited
- end
- def hasCycle?(obstacle)
- grid = @grid.dup << obstacle
- pos = @start
- dir = 0
- visited = Set[[pos, dir]]
- while inbounds?(pos) do
- until grid.include?(pos + @dirs[dir]) do
- return true if visited.include?([pos + @dirs[dir], dir])
- pos += @dirs[dir]
- break unless inbounds?(pos)
- visited << [pos, dir]
- end
- dir = (dir+1) % 4
- end
- false
- end
- path = findPath
- puts "Part 1: #{path.size}"
- path.delete(@start)
- obstacles = path.map{|p| hasCycle?(p)}.count(true)
- puts "Part 2: #{obstacles}"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement