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