[2024][ruby][6.2]

main
Alpha Chen 6 days ago
parent 2a88deecc7
commit a7822ce4f1
Signed by: alpha
SSH Key Fingerprint: SHA256:3fOT8fiYQG/aK9ntivV3Bqtg8AYQ7q4nV6ZgihOA20g

@ -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__
....#.....

Loading…
Cancel
Save