[2024][ruby][18.2]

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

@ -1,13 +1,11 @@
falling_bytes = DATA.read.scan(/\d+/).map(&:to_i).each_slice(2).to_a 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
deltas = [[-1,0], [1,0], [0,-1], [0,1]]
def path(corrupted)
start = [0,0] start = [0,0]
stop = [max,max] stop = [MAX,MAX]
scores = Hash.new(Float::INFINITY) scores = Hash.new(Float::INFINITY)
scores[start] = 0 scores[start] = 0
@ -17,10 +15,10 @@ until scores.has_key?(stop)
current = frontier.shift current = frontier.shift
visited << current visited << current
neighbors = deltas.map {|delta| neighbors = DELTAS.map {|delta|
current.zip(delta).map { _1 + _2 } current.zip(delta).map { _1 + _2 }
}.select {|xy| }.select {|xy|
xy.all? { (0..max).cover?(_1) } xy.all? { (0..MAX).cover?(_1) }
}.reject {|xy| }.reject {|xy|
corrupted.include?(xy) || visited.include?(xy) corrupted.include?(xy) || visited.include?(xy)
} }
@ -34,7 +32,17 @@ until scores.has_key?(stop)
frontier.sort_by! { scores.fetch(_1) } frontier.sort_by! { scores.fetch(_1) }
end end
pp scores.fetch(stop) scores.fetch(stop)
end
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__ __END__
54,47 54,47

Loading…
Cancel
Save