[2024][ruby][18.2]

main
Alpha Chen 5 days ago
parent fb7247cef1
commit 5126ad233a
Signed by: alpha
SSH Key Fingerprint: SHA256:3fOT8fiYQG/aK9ntivV3Bqtg8AYQ7q4nV6ZgihOA20g

@ -1,40 +1,48 @@
falling_bytes = DATA.read.scan(/\d+/).map(&:to_i).each_slice(2).to_a
corrupted = falling_bytes[0,1024]
MAX = 70
DELTAS = [[-1,0], [1,0], [0,-1], [0,1]]
max = 70
def path(corrupted)
start = [0,0]
stop = [MAX,MAX]
deltas = [[-1,0], [1,0], [0,-1], [0,1]]
scores = Hash.new(Float::INFINITY)
scores[start] = 0
frontier = [start]
visited = Set.new
until scores.has_key?(stop)
current = frontier.shift
visited << current
start = [0,0]
stop = [max,max]
neighbors = DELTAS.map {|delta|
current.zip(delta).map { _1 + _2 }
}.select {|xy|
xy.all? { (0..MAX).cover?(_1) }
}.reject {|xy|
corrupted.include?(xy) || visited.include?(xy)
}
neighbors.each do |xy|
scores[xy] = [scores[xy], scores.fetch(current) + 1].min
end
scores = Hash.new(Float::INFINITY)
scores[start] = 0
frontier = [start]
visited = Set.new
until scores.has_key?(stop)
current = frontier.shift
visited << current
frontier.concat(neighbors)
neighbors = deltas.map {|delta|
current.zip(delta).map { _1 + _2 }
}.select {|xy|
xy.all? { (0..max).cover?(_1) }
}.reject {|xy|
corrupted.include?(xy) || visited.include?(xy)
}
neighbors.each do |xy|
scores[xy] = [scores[xy], scores.fetch(current) + 1].min
frontier.uniq!
frontier.sort_by! { scores.fetch(_1) }
end
frontier.concat(neighbors)
frontier.uniq!
frontier.sort_by! { scores.fetch(_1) }
scores.fetch(stop)
end
pp scores.fetch(stop)
i = (0...falling_bytes.length).bsearch {|i|
begin
path(falling_bytes[0,i]) && false
rescue
true
end
}
puts falling_bytes.fetch(i-1).join(?,)
__END__
54,47

Loading…
Cancel
Save