72 lines
1.5 KiB
Ruby
Executable file
72 lines
1.5 KiB
Ruby
Executable file
#!/usr/bin/env ruby
|
|
|
|
require "debug"
|
|
|
|
problem = 10
|
|
@input = File.readlines("#{problem}.input")
|
|
#@input = DATA.read.split("\n")
|
|
.map { _1.strip.chars.map { |c| c == "." ? "." : c.to_i }}
|
|
@debug = @input.size < 20
|
|
#@debug = true
|
|
|
|
def bounded?(point)
|
|
point.first >= 0 &&
|
|
point.last >= 0 &&
|
|
point.first < @input.size &&
|
|
point.last < @input.first.size
|
|
end
|
|
|
|
def traverse(start, height = 1)
|
|
result = [
|
|
[start.first - 1, start.last],
|
|
[start.first + 1, start.last],
|
|
[start.first, start.last - 1],
|
|
[start.first, start.last + 1]
|
|
].map do |point|
|
|
if !bounded?(point) || @input.dig(*point) == "."
|
|
[]
|
|
elsif @input.dig(*point) == 9 && height == 9
|
|
if @debug
|
|
@paths[point.first][point.last] = @input.dig(*point)
|
|
end
|
|
[point]
|
|
elsif @input.dig(*point) == height
|
|
traverse(point, height + 1).flatten(1)
|
|
else # if @input.dig(*point) != height
|
|
[]
|
|
end
|
|
end
|
|
|
|
if result.size > 0 && @debug
|
|
@paths[start.first][start.last] = @input.dig(*start)
|
|
end
|
|
|
|
result
|
|
end
|
|
|
|
if @debug
|
|
@paths = Array.new(@input.size) { Array.new(@input.first.size) { "." } }
|
|
end
|
|
|
|
result = (0...@input.size).flat_map do |x|
|
|
(0...@input.first.size).map do |y|
|
|
[[x, y], traverse([x, y]).flatten(1)] if @input[x][y] == 0
|
|
end.compact
|
|
end.to_h
|
|
|
|
if @debug
|
|
puts @paths.map(&:join).join("\n")
|
|
end
|
|
|
|
p result.values.map { _1.uniq.size }.sum
|
|
p result.values.map { _1.size }.sum
|
|
|
|
__END__
|
|
89010123
|
|
78121874
|
|
87430965
|
|
96549874
|
|
45678903
|
|
32019012
|
|
01329801
|
|
10456732
|