|
|
|
@ -5,33 +5,56 @@ input = DATA.readlines(chomp: true)
|
|
|
|
|
}
|
|
|
|
|
}.to_h
|
|
|
|
|
|
|
|
|
|
turns = {
|
|
|
|
|
TURNS = {
|
|
|
|
|
[-1,0] => [0,1],
|
|
|
|
|
[0,1] => [1,0],
|
|
|
|
|
[1,0] => [0,-1],
|
|
|
|
|
[0,-1] => [-1,0],
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
start = input.find { _2 == ?^ }.first
|
|
|
|
|
dir = [-1,0]
|
|
|
|
|
path = [start]
|
|
|
|
|
loop do
|
|
|
|
|
peek = path.last.zip(dir).map {|i,di| i + di }
|
|
|
|
|
case input.fetch(peek, nil)
|
|
|
|
|
when ?.
|
|
|
|
|
path << peek
|
|
|
|
|
when ?^
|
|
|
|
|
path << peek
|
|
|
|
|
Loop = Class.new(Exception)
|
|
|
|
|
|
|
|
|
|
def patrol(lab)
|
|
|
|
|
start = lab.find { _2 == ?^ }.first
|
|
|
|
|
path = [[start, [-1,0]]]
|
|
|
|
|
seen = path.to_set
|
|
|
|
|
loop do
|
|
|
|
|
pos, dir = path.last
|
|
|
|
|
peek = pos.zip(dir).map {|i,di| i + di }
|
|
|
|
|
raise Loop if seen.include?([peek, dir])
|
|
|
|
|
seen << [peek, dir]
|
|
|
|
|
|
|
|
|
|
case lab.fetch(peek, nil)
|
|
|
|
|
when ?., ?^
|
|
|
|
|
path << [peek, dir]
|
|
|
|
|
when ?#
|
|
|
|
|
dir = turns.fetch(dir)
|
|
|
|
|
path.last[1] = TURNS.fetch(dir)
|
|
|
|
|
when nil
|
|
|
|
|
break
|
|
|
|
|
return path
|
|
|
|
|
else
|
|
|
|
|
fail
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
pp path.to_set.length
|
|
|
|
|
def loop?(lab)
|
|
|
|
|
patrol(lab)
|
|
|
|
|
false
|
|
|
|
|
rescue Loop
|
|
|
|
|
true
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
pp patrol(input).map(&:first).to_set.length
|
|
|
|
|
|
|
|
|
|
pp input
|
|
|
|
|
.select { _2 == ?. }
|
|
|
|
|
.select {|xy, _|
|
|
|
|
|
# pp xy
|
|
|
|
|
input[xy] = ?#
|
|
|
|
|
res = loop?(input)
|
|
|
|
|
input[xy] = ?.
|
|
|
|
|
res
|
|
|
|
|
}.size
|
|
|
|
|
|
|
|
|
|
__END__
|
|
|
|
|
....#.....
|
|
|
|
|